0
from gtts import gTTS
import os
import subprocess
tts = gTTS(text='Hello World', lang='en') //TEXT TO SPEECH
tts.save("audio.mp3") //Hello world will be saved in audio.mp3
os.system("mpg321 audio.mp3") <-- I want to play the audio file in python(not to call any other music player)

I want to play the audio.mp3 file, but nothing happnes (cmd comes up and goes away in 1 second)

Nehal J Wani
  • 16,071
  • 3
  • 64
  • 89
Udantha
  • 77
  • 1
  • 4
  • 12

2 Answers2

2

You should specify the absolute path to your mp3 file. (like C:/songs/song.mp3)

Also, consider using pygame if it won't work. Have you tried a solution from this question? Playing mp3 song on python

Community
  • 1
  • 1
Usmiech
  • 181
  • 3
  • 12
2

Your code seems to be fine; I can repeat it on my system except that I don't have the application mpg321 installed so I use play from the sox family instead and it works correctly even without a full path to the file name.

See if you have sox installed on your machine, and if so try playing the file with play which will be able to pick up the parameters of the mp3 from the file. Alternatively use a different player, but be careful to use one that can read mp3 files properly otherwise you will get a blast from the loudspeakers.

Colin Beckingham
  • 527
  • 3
  • 11