How To Play Music/mp3 Using Python

Working with media handling is the funniest part of development. In this post, we are going to see how we can play music/mp3 using python. There are several Libraries for the same, but we are going to use the fastest and easiest library for this i.e python-vlc.

Setup:

Step 1 : Install VLC media player

VLC Media player is a popular music player used to play videos and music. It also gives support to the python programmers to use its APIs and handle media easily. Please Install Windows 64bit VLC Media Player from this link.

Step 2: Install python-vlc Library

As discussed we have to install python-vlc library. The preferred install method is through PyPI:

pip install python-vlc

or (if you are using python3):

pip3 install python-vlc

You can also download the vlc.py module from the Git repository. It only depends on ctypes (standard module in python >= 2.5). Put the module in some place accessible by python (either next to your application, or in a directory from sys.path).

Alternatively, you can generate it by yourself using the generate.py program and accompanying files in the vlc source tree (see [1]).

Note that this only installs the python module itself, which depends on the availability of the libvlc libraries. You must also install VLC itself to get these libraries.

Usage:

First we are going to import and instantiate the media file.

import vlc 
player = vlc.MediaPlayer("/path/to/music.mp3")

Then we can play the music using

player.play()

Pause the music

player.pause()

Stop the music

player.stop()

Full Code for testing:

import time
import vlc 
player = vlc.MediaPlayer("/path/to/music.mp3")
#start the music
player.play()
#listen music for 10 seconds
time.sleep(10)
#stop the music
player.stop()

Hope you have liked the video and enjoy the learning. let me know your updates by commenting below.

If you get any doubts, feel free to contact me.

Connect with us on social media to get updates for future posts. YouTubeInstagramFacebook.

Recommended Tutorials:

Summary
Article Name
How to play music using python
Description
Working with media handling is the funniest part of development. In this post, we are going to see how we can play music/mp3 using python. There are several Libraries for the same, but we are going to use the fastest and easiest library for this i.e python-vlc.
Author
Copy link
Powered by Social Snap