Os is a standard Python module that doesn’t need to be installed. Lastly, run the code and your audio will begin playing.Import playsound aysound(‘testaudio.mp3’) Next, enter the following piece of code:.First, enter the following in the Terminal:.Playing Sound, Audio, or MP3 Files in Python on Linux Using Playsound Then, the program will finish executing after 4 seconds as specified by the time.sleep() function. Import pygame import time () # initialize mixer module ('testaudio.mp3') () print("Audio will play for 4 seconds") time.sleep(4)įirst, the audio will start playing, and the result of the print function will be shown on the screen. Second, enter the following in your code editor:.Pygame is a game development module that is cross-platform and enables us to use Python for graphics and audio purposes. The program will finish executing right after as that is the time we specify in time.sleep(4). At the same time, the result of the print function is shown to indicate that the audio is 4 seconds long. When running the program, it should look something like this:Ī terminal window will appear and your audio should start playing. The time.sleep(4) function tells the program to delay execution for 4 seconds before we call the p.stop() function. Import vlc import time p = vlc.MediaPlayer(‘testaudio.mp3’) p.play() print("The audio will finish playing in 4 seconds") time.sleep(4) p.stop() In your code editor, enter the following:.Otherwise, the file will finish executing immediately without our audio being played. The time module enables us to determine how long we want the file to delay execution so we can hear our audio being played. We will be using another module alongside the Python VLC module in order to delay the execution of the file.VLC has a python module that enables us to play mp3 files.Ĭonfirm the installation by entering: pip list VLC is an open-source and cross-platform media player that can be used to play audio and video files. After running the code, your audio will begin playing and will stop automatically depending on the length of your mp3 file.In the code editor, enter the following:.You can also enter pip list in the terminal to have a look at all the installed packages and their versions. If playsound is installed, there should be no errors in the output. If you would like to double-check that you’ve properly installed the module, enter python in the terminal to access the python terminal and then import playsound. First, enter the following command in the Command Prompt Terminal :.Playsound is a cross-platform Python module with no dependencies and a single function. Playing Sound, Audio, or MP3 Files in Python on Windows Using Playsound Playing Sound, Audio, or MP3 Files in Python on macOS.Playing Sound, Audio, or MP3 Files in Python on Linux.Playing Sound, Audio, or MP3 Files in Python on Windows.py file which we will be using to run the code. Note: In this guide, a 4-second MP3 file is used named testaudio.mp3 and it is located in the same directory as the. In this guide, we will be going over the steps to play sound, audio, or MP3 files in Python on Windows, Linux, and macOS. Luckily, playing audio in Python is straightforward and can be done with a few lines of code. Therefore, you need to have Python and PIP installed on your system, before installing these Python modules. Most of the modules that we will be using don’t come pre-installed with Python, which means we will have to install them ourselves. We can also play it natively on macOS and Linux as we will see later on. These include modules such as playsound, VLC, pygame, mpg123, etc. There are numerous ways for us to play sound, audio, or MP3 files using the Python programming language. If you’re trying to play, read, open or run sound, audio or MP3 files using Python, look no further, this is the perfect guide for you whether you’re on a Windows, Linux or macOS machine!
0 Comments
Leave a Reply. |