Package: audioplayer
AudioPlayeraudioplayeris a cross platform Python 3 package for playing sounds (mp3, wav, ...). It provides the key features of an audio player, such as opening a media file, playing (loop/block), pausing, resuming, stopping, and setting the playback volume.I created this package to provide sound functionality to my game librarygame2dboard, but I decided to publish it separately.Suported systems (seefull list):GNU/Linux (PC, Raspberry Pi, ...)WindowsmacOSInspired by (and with a few lines of codes from) theplaysound module.InstallPrerequisitesGNU/LinuxIn Linux, you need to install PyGObject and others dependencies.Ubuntu/Debian/Raspberry Pi OS:sudoapt-getinstallpython-gst-1.0\gir1.2-gstreamer-1.0\gstreamer1.0-tools\gir1.2-gst-plugins-base-1.0gstreamer1.0-plugins-good\gstreamer1.0-plugins-uglyRedhat/Centos/Fedora:sudoyuminstall-ypython-gstreamer1\gstreamer1-plugins-good\gstreamer1-plugins-uglymacOSIn macOS, you need to install PyObjC bridge.pip3installPyObjC--userInstallThe recommended way to installaudioplayeris using the Pythonpip(orpip3) installer.pip3 install audioplayerIf you don't have administrator privileges, install in your home folder.pip3 install audioplayer --userYou can install the latest release by cloning this repository.gitclonehttps://github.com/mjbrusso/audioplayer.gitcdaudioplayer
python3setup.pyinstall--userUsageThe API is documentedbellowand within the docstrings.After install, you can use this code to test (replace "path/to/somemusic.mp3"):fromaudioplayerimportAudioPlayer# Playback stops when the object is destroyed (GC'ed), so save a reference to the object for non-blocking playback.AudioPlayer("path/to/somemusic.mp3").play(block=True)APICreationaudioplayer.AudioPlayer(filename)Creates the player.filename:str– The file name with extension  (.mp3, .wav, ...)Raise:FileNotFoundError():  The file does not exist.Propertiesfilename:str(readonly)The file name as provided in the constructor.fullfilename:str(readonly)The file name with full path.volume:intGets or sets the current volume (in %) of the audio (0 — 100)Methodsplay(loop=False, block=False)Starts audio playback.loop(bool) – Sets whether to repeat the track automatically when finished.block(bool) – If true, blocks the thread until playback ends.Raise:AudioPlayerError(): Failed to play.pause()Pauses audio playback.resume()Resumes audio playback.stop()Stops audio playback. Can play again.close()Closes device, releasing resources. Can't play again.Suported SystemsaudioPlayerhas been tested on the following platforms:OSDetailsmp3wavoggmidGNU/LinuxMint 19 (Cinnamon)✓✓✓✕GNU/LinuxXubuntu 20.04✓✓✓✓GNU/LinuxRaspberry Pi OS✓✓✓✓WindowsWindows 10 x64✓✓✕✓macOSCatalina (Python 3.8)✓✓✕✕?:Not yet testedLet me know if you are using on another system/distro/version!What's in the roadmap?.seek(position): Moves playback to the specified position..state:  Current state (playing, stopped, paused, ...).speed = value: Gets/sets playback speed.Callbacks: when state changed, when position changed by a delta, ...How to ContributeSubmitting an issueUse theissue trackerto submit bug reports and features or enhancements requests.TranslatingYou can contribute by translating this document into other languages ​​(exceptenandpt_br).Submitting a pull requestIf you can improve anything in this project, feel free to add apull request.Licenseaudioplayeris underMIT license. It can be reused within proprietary software provided that all copies of the licensed software include a copy of the MIT License terms and the copyright notice.