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(channels, sample_size, sample_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(width, height, sample_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(filename, file=None, streaming=True, decoder=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.media, Pyglet 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 DSA, Competitive Programming and MERN Stack Web Development. Do upvote our blog to help other ninjas grow.
Happy Learning!