PYGLET – Media Player - GeeksforGeeks

27 Aug, 2021
In this article we will see how we can create a simple media player in 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). In order to load a file i.e resource we use resource module of pyglet. 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. The Player class implements the best available audio device.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 play a media in pyglet we have to do the following 1. Create pyglet window 2. Create a player object 3. Load the media by player from the path of media 4. Add loaded media in the queue of player 5. Play the video 6. Create a key press event handler which is used to pause and resume the video  
Below is the implementation  
Python3
# importing pyglet moduleimport pyglet # width of windowwidth = 500   # height of windowheight = 500   # caption i.e title of the windowtitle = "Geeksforgeeks"   # creating a windowwindow = pyglet.window.Window(width, height, title)  # video pathvidPath ="media.mp4" # creating a media player objectplayer = pyglet.media.Player() # creating a source objectsource = pyglet.media.StreamingSource() # load the media from the sourceMediaLoad = pyglet.media.load(vidPath) # add this media in the queueplayer.queue(MediaLoad) # play the videoplayer.play() # on draw event@window.eventdef 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.eventdef on_key_press(symbol, modifier):       # key "p" get press    if symbol == pyglet.window.key.P:                 # printing the message        print("Key : P is pressed")                 # pause the video        player.pause()                 # printing message        print("Video is paused")                      # key "r" get press    if symbol == pyglet.window.key.R:                 # printing the message        print("Key : R is pressed")                 # resume the video        player.play()                 # printing message        print("Video is resumed") # run the pyglet applicationpyglet.app.run()
Output :  
 
Key : P is pressed
Video is paused
Key : R is pressed
Video is resumed
Key : P is pressed
Video is paused
Key : R is pressed
Video is resumed
 
surinderdawra388
rajeev0719singh
Python-gui
Python-Pyglet
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()