Monday, November 18, 2024
Google search engine
HomeLanguagesPYGLET – Play Next Media in Player

PYGLET – Play Next Media in Player

In this article we will see how we can play next media which is in queue of player of PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc. A window is a “heavyweight” object occupying operating system resources. Windows may appear as floating regions or can be set to fill an entire screen (fullscreen). This module allows applications to specify a search path for resources. Pyglet can play WAV files, and if FFmpeg is installed, many other audio and video formats. Playback is handled by the Player class, which reads raw data from Source objects and provides methods for pausing, seeking, adjusting the volume, and so on. Next media will move player immediately to the next source in the current playlist. If the playlist is empty, discard it and check if another playlist is queued. There may be a gap in playback while the audio buffer is refilled.
We can create a window and player object with the help of commands given below 
 

# creating a window
window = pyglet.window.Window(width, height, title)

# creating a player for media
player = pyglet.media.Player()

 

In order to do this we use next_source play method with the player object
Syntax : player.next_source()
Argument : It takes no argument
Return : It returns None 
 

Below is the implementation 
 

Python3




# importing pyglet module
import pyglet
 
# width of window
width = 800
   
# height of window
height = 500
   
# caption i.e title of the window
title = "Geeksforneveropen"
   
# creating a window
window = pyglet.window.Window(width, height, title)
 
 
# video path
vidPath ="gfg.mp4"
 
# creating a media player object
player = pyglet.media.Player()
 
# creating a source object
source = pyglet.media.StreamingSource()
 
# load the media from the source
MediaLoad = pyglet.media.load(vidPath)
 
# add this media in the queue
player.queue(MediaLoad)
 
# loading a new media
media = pyglet.media.load("media.mp4")
 
# add this media in the queue
player.queue(media)
 
 
# play the video
player.play()
 
# on draw event
@window.event
def on_draw():
     
    # clea the window
    window.clear()
     
    # if player source exist
    # and video format exist
    if player.source and player.source.video_format:
         
        # get the texture of video and
        # make surface to display on the screen
        player.get_texture().blit(0, 0)
         
         
# key press event    
@window.event
def on_key_press(symbol, modifier):
   
    # key "p" get press
    if symbol == pyglet.window.key.P:
         
        # pause the video
        player.pause()
         
        # printing message
        print("Video is paused")
         
         
    # key "r" get press
    if symbol == pyglet.window.key.R:
         
        # resume the video
        player.play()
         
        # printing message
        print("Video is resumed")
 
# play the next source
player.next_source()
         
 
# run the pyglet application
pyglet.app.run()


Output : 
 

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments