I am trying to play the audio files using Python. Below is my code:
from pygame import mixer
from time import sleep
from playsound import playsound
mixer.init()
# mixer.music.load("test1.mp3")
# mixer.music.play()
# sleep(1)
playsound("test.mp3")
I tried 2 methods to play audio:
- playsound
- pygames
with both the methods I was able to play the below mentioned audio. https://drive.google.com/file/d/1-QQmMJnaK20L47DZGZS0MGZHnMK1uxdL/view?usp=sharing (filename is welcome5.mp3)--> able to play
but was not able to play the below audio given below:
https://drive.google.com/file/d/1Mh8iMM026aSKaBD2iEUB9R5dceRcNa4w/view?usp=sharing (file name is test.mp3)
With playsound I am getting the below error:
Traceback (most recent call last):
File "c:\Users\SANKETH\PycharmProjects\Practice\Files\tests\test.py", line 9, in <module>
playsound("test.mp3")
File "C:\Users\SANKETH\AppData\Local\Programs\Python\Python38-32\lib\site-packages\playsound.py", line 35, in _playsoundWin
winCommand('open "' + sound + '" alias', alias)
File "C:\Users\SANKETH\AppData\Local\Programs\Python\Python38-32\lib\site-packages\playsound.py", line 31, in winCommand
raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
Error 277 for command:
open "test.mp3" alias playsound_0.8194752932436311
A problem occurred in initializing MCI.
in case of pygame I am getting the below error:
Note: Illegal Audio-MPEG-Header 0x00000000 at offset 2650.
Note: Trying to resync...
Note: Skipped 10 bytes in input.
[d:\build\libmpg123\mpg123-1.25.10\src\libmpg123\layer1.c:30] error: Illegal bit allocation value.
[d:\build\libmpg123\mpg123-1.25.10\src\libmpg123\layer1.c:174] error: Aborting layer I decoding after step one.
below are the details of the libraries or packages installed from pip:
$ pip3 list|grep -i "playsound"
playsound 1.2.2
SANKETH@DESKTOP-ULOB0S7 MINGW64 ~/PycharmProjects/Practice/Files/tests (master)
$ pip3 list|grep -i "pygame"
pygame 1.9.6
names of the files:
$ ls -l|grep -i "test"
-rw-r--r-- 1 SANKETH 197121 9661986 Aug 31 02:55 test.mp3
-rw-r--r-- 1 SANKETH 197121 183 Aug 31 21:18 test.py
-rw-r--r-- 1 SANKETH 197121 1772 Aug 30 22:32 test.txt
-rw-r--r-- 1 SANKETH 197121 58890 Aug 31 03:14 test1.mp3
I tried with your code in my local machine and it works totally fine to run this script all you need is to install pygame and playsound using pip
after that you need to change an mp3 file to name test.mp3 >> (when I try this in my local windows machine as "test.mp3" it doesn't work then I changed it to "test" then it started working )
I think you better check with the file name in Linux I think you should use the cli for listing the file name