In this Python Pyglet article we are going to talk about How To Play Mp3 Songs In Python Pyglet. First of all let’s talk about Python Pyglet.
What is Python Pyglet?
Pyglet is a cross platform multimedia library for Python. It provides an easy interface for creating games, interactive applications, multimedia software and many more. Pyglet is built on top of OpenGL and provides bindings for audio, video and input devices, and this is good for different types of multimedia applications.
Key features of Pyglet:
- Graphics: Pyglet provides powerful graphics module for rendering 2D and 3D graphics using OpenGL. It supports hardware-accelerated rendering and provides utilities for handling textures, sprites, shapes and transformations.
- Audio: Pyglet includes audio module for playing and managing audio files. It supports different audio formats and provides features for controlling playback, adjusting volume, and handling sound effects.
- Video: Pyglet has built-in support for playing video files using FFmpeg. It allows you to load, play and manipulate video files inside your Python applications.
- Input Handling: Pyglet provides input handling capabilities for handling user interactions such as keyboard, mouse and joystick events. It allows you to easily detect and respond to user input in your applications.
- Windowing: Pyglet includes a windowing module for creating and managing windows and OpenGL contexts. It allows you to create customizable windows with support for resizing, fullscreen mode, and multiple monitors.
How to Install Pyglet?
You can install Python Pyglet using pip like this
1 |
pip install pyglet |
How to Play Mp3 Songs in Python Pyglet?
So now this the complete source code for this article
1 2 3 4 5 6 7 |
import pyglet music = pyglet.resource.media('guitar.mp3', streaming=False) music.play() pyglet.app.run() |
First we have imported our Pyglet library.
1 |
import pyglet |
This line of code is for getting the media from the resource, make sure that you have an Mp3 Song in your project directory. as i have already added a guitar mp3.
1 |
music = pyglet.resource.media('guitar.mp3', streaming=False) |
This line of code is for playing the Mp3 Songs
1 |
music.play() |
And finally
1 |
pyglet.app.run() |
This will enter pyglet’s default event loop, and let pyglet respond to application events such as the mouse and keyboard. Your event handlers will now be called as required, and the run() method will return only when all application windows have been closed.
If you are coming from another library, you may be used to writing your own event loop. This is possible to do with pyglet as well, but it is generally discouraged; see The application event loop for details.
Subscribe and Get Free Video Courses & Articles in your Email