Table of contents
1.
Introduction
2.
What is pyglet.media?
3.
Classes
3.1.
class Player
3.2.
class PlayerGroup(players)
3.3.
classAudioFormat(channels, sample_size, sample_rate)
3.4.
classVideoFormat(width, height, sample_aspect=1.0)
3.5.
classSourceInfo
3.6.
classSource
4.
Functions
4.1.
get_audio_driver( )
4.2.
load(filename, file=None, streaming=True, decoder=None)
4.3.
have_ffmpeg()
5.
Exceptions
6.
Frequently Asked Questions
6.1.
How to play music continuously in pyglet?
6.2.
Why is pyglet used?
6.3.
How do you play Pyglet videos?
7.
Conclusion
Last Updated: Mar 27, 2024

pyglet.media

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Python has become one of the most necessary programming languages to learn these days. Python is being used everywhere, be it game development, web development, or software development. Pyglet is a module in python using which a user can develop many interfaces. In this article, we will discuss the pyglet media player and its classes, functions and exceptions in detail. Let us dive deeper into the topic.

What is pyglet.media?

Pyglet is a module in python using which a user can develop many interfaces. Pyglet is a compelling library using which we can develop visually rich GUI interfaces like Games and multimedia. pyglet.media is used for audio and video playback, i.e., replaying the audio or video as many times as the user wants to check a recorded message or simply monitor the quality. pyglet can play WAV files. pyglet can also play audio and videos in many different formats if the FFmpeg is installed. The player class handles the playback by reading raw data from source objects and providing methods for volume adjustment, playing, seeking, and so on. The class player implements the best audio device.

player = Player()

The Source decodes arbitrary audio and video files and gets associated with a single player by “queueing” it:

source = load('background_music.mp3')
player.queue(source)

The Player is used to control playback. If the Source has some video, the Source.video_format() attribute will be non-None, and the Player.texture attribute will include the current video image synchronized to the audio.

Classes

class Player

class Player is a high-level video and sound player. It has various methods like:

  • play( ): This method is used to start playing the source. If the player is already playing, this has no effect.
  • pause( ): This method is used to stop playing the source. If the Player has already stopped playing, this has no effect.
  • queue( ): This method is used to queue a source to the current playlist. The player will start to play or pause depending on its playing attribute; if the player has no source.
  • seek_next_frame(): This method goes one video frame forward in the current source.
  • get_texture(): This method gets the texture for the current video frame. It returns pyglet.image.Texture. If there is no video in the current source, its return value will be none.
  • delete(): This method deletes the resources in the player.

class PlayerGroup(players)

This class makes a group of players play and pause simultaneously. The players in the group must not belong to any other group at a time.
Parameters:  players(List[Player]) – List of players in this group.
The methods of this class are:

  • play( ): This method plays all the players in the group simultaneously.
  • pause(): This pauses all the players in the group simultaneously.

classAudioFormat(channelssample_sizesample_rate)

Sources provide an instance of this class with audio tracks. You should not modify the fields, as they are used internally to describe the format of data provided by the source.

  • Parameters: channels (int) – The number of channels: 1 for mono or 2 for stereo.
  • sample_size (int) – Bits per sample; only 8 or 16 are supported.
  • sample_rate (int) – Samples per second (in Hertz).

classVideoFormat(widthheightsample_aspect=1.0)

Sources provide an instance of this class with a video stream. The fields should not be modified. The parameters of this class are:

  • width (int) – Width of the video image, in pixels.
  • height (int) – Height of video image, in pixels.
  • sample_aspect (float) – Aspect ratio (width over height) of a single video pixel.
  • frame_rate (float) – Frame rate (frames per second) of the video.

classSourceInfo

This class provides the Source metadata information. Fields are zero if the information is not available. The parameters of the class are:

  • title (str) – Title
  • author (str) – Author
  • copyright (str) – Copyright statement
  • comment (str) – Comment
  • album (str) – Album name
  • year (int) – Year
  • track (int) – Track number
  • genre (str) – Genre

classSource

This class provides an audio or video source or both. The parameters of this class are :

  • audio_format(AudioFormat) – provides the audio format of the current source or none if the source is silent.
  • video_format(VideoFormat) – provides the format of the video in this source or none if there is no video.
  • info(SourceInfo) – provides the source metadata such as title, artist, etc.; or None if the information is not available.

Functions

get_audio_driver( )

This function gets the preferred audio driver for the current platform. If the platform supports more than one audio driver, the application can give its preference with pyglet.options audio keyword. For example, It can be used in a code in the following format:

Source = pyglet.media.get_audio_driver()

Returns: The concrete implementation of the preferred audio driver for this platform.
Return type: AbstractAudioDriver

load(filenamefile=Nonestreaming=Truedecoder=None)

This function is used to load a source from a file. For example, it can be used in a code in the following way:

load_sound_library()
    source = pyglet.media.load(filename, streaming=False)
    return source

Return type: StreamingSource or Source

All the decoders registered for the filename extension are tried. If none succeed, the exception from the first decoder is raised. The parameters of this class are:

  • filename (str) – Used to guess the media format and load the file if the file is unspecified.
  • file (file-like object or None) – Source of media data in any supported format.

have_ffmpeg()

This function checks if the FFmpeg library is available. For example, It can be used in a code in the following format:

print(“FFmpeg DDLs found: “, pyglet.media.have_ffmpeg())

Returns: True if FFmpeg is found otherwise false.

Exceptions

exception CannotSeekException

exception MediaDecodeException

exception MediaEncodeException

exception MediaException

exception MediaFormatException

Frequently Asked Questions

How to play music continuously in pyglet?

You can use a Player in Pyglet to play a sound continuously. First create a player and then queue the song. For example,

player = pyglet.media.Player()
sound = pyglet.media.load('lines.mp3')
player.queue(sound) 
player.eos_action = pyglet.media.SourceGroup.loop
player.play()

Why is pyglet used?

Pyglet is a multimedia library for Python used to build games and other visually rich applications. 

How do you play Pyglet videos?

First, create a pyglet window and a player object. Load the media in the player and queue it. Then you can play the video. Create a event handler to pause and resume the video.

Conclusion

In this article, we have extensively discussed pyglet media player and its classes, functions and exceptions in detail. Having gone through this article, I am sure you must be excited to read similar blogs. Coding Ninjas has got you covered. Here are some similar blogs to redirect: What is pyglet.mediaPyglet and Pyglet library.  We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out our Coding Ninjas Blog site and visit our Library. Here are some courses provided by Coding Ninjas: Basics of C++ with DSACompetitive Programming and MERN Stack Web Development. Do upvote our blog to help other ninjas grow.

Happy Learning! 

Live masterclass