Not able to play audio file in python

0

I am trying to play the audio files using python.

Below here 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.

  1. playsound
  2. 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)

it would be of great help if someone will help me play the audio for the below, through any methods in python. I want to pause the audio at any given moment.

link for audio file for failed case: https://drive.google.com/file/d/1Mh8iMM026aSKaBD2iEUB9R5dceRcNa4w/view?usp=sharing(File which i want to play and pause at any given moment. Filename is test.mp3)

With play sound 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.

All helps from expert people will greatly be appreciated.

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

Thank you

python
pygame
playsound
gtts
asked on Stack Overflow Aug 31, 2020 by sanketh s • edited Aug 31, 2020 by sanketh s

1 Answer

0

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

pip install pygame
pip install playsound

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

answered on Stack Overflow Aug 31, 2020 by vgeorge

User contributions licensed under CC BY-SA 3.0