ObjectivePlay audio with VLC in Python.
DistributionsThis will work on any Linux distribution
RequirementsA working Linux install with Python and VLC.
- # - requires given command to be executed with root privileges either directly as a root user or by use of
- $ - given command to be executed as a regular non-privileged user
IntroductionThere are plenty of ways to play audio files with Python. It really depends on your application, but the easiest way, by far, is to use the bindings for VLC to control VLC with Python, and play your files.
With VLC, you don't need to worry about codecs and file support. It also doesn't require too many complicated methods, and/or objects. So, for simple audio playback, VLC is best.
Get The VLC BindingsThe VLC bindings are actually developed and maintained by VLC. That said, the easiest way is still to use
# pip install python-vlcOf course, if this is for a single project, use
Set Up Your FileCreating your file is very simple. You only need to import the VLC module.
import VLCThat's really all. You can use the module to create
MediaPlayerinstances, and that's what's necessary to play audio.
Create A Media Player ObjectAgain, the VLC module is super easy to use. You only need to instantiate a
MediaPlayerobject and pass it the audio file that you want to play. VLC can handle virtually any file type, so you don't need to worry about compatibility.
player = vlc.MediaPlayer("/path/to/file.flac")
Play A SongPlaying a file from an existing object is even easier. You only need to call the
playmethod on the object, and Python will begin playing it. When the playback finishes, it will stop. There's no looping or any nonsense like that.
Stopping And PauseThe VLC bindings make it easy to stop or pause a file once you've started playing it too. There is a
pausemethod that will pause playback if the file is playing.
player.pause()If the player is already paused, calling the method again will resume playback. To stop a file altogether, call the
Looping And "Playlists"You can actually create pseudo-playlists with this, and loop through the songs that you've added. It would only take a basic
That's obviously very rudimentary, but you can see how Python can script VLC.
playlist = ['/path/to/song1.flac', '/path/to/song2.flac', 'path/to/song3.flac'] for song in playlist: player = vlc.MediaPlayer(song) player.play()
Closing ThoughtsVLC isn't the only solution for playing audio with Python, and it certainly isn't the best in every situation, but it is very good for a lot of basic use cases. The greatest bonus of using VLC is the unbeatable simplicity.
- Install the Python VLC bindings with
pipin a virtual environment.
- Create a Python file and import the VLC bindings.
- Instantiate a player object to play a file.
- Play that file.
- Play the file again. Pause and resume playback.
- Create a loop to play multiple files in order.
- Challenge: Generate a list of files using Python modules to interact with directories on your system. Play them as a playlist.