我在 python 中使用了 os.system("start audio.mp3") 并且它起作用了。但它不会自行关闭,音频会在其他屏幕上打开

I used os.system("start audio.mp3") in python and it worked. But it does not close itself and the audio opens on other screens

我打开音频文件,但它没有自行关闭,播放器在其他屏幕上打开。怎么打开才能最小化。然后我想让它自己关闭。

from gtts import gTTS
import os

def speech():
   language = 'tr'
   myText = "Welcome"
   output = gTTS(text=myText, lang=language, slow=False)
   output.save("audio.mp3")
   os.system("start audio.mp3")
   time.sleep(5)
   os.system("close audio.mp3")
.
.
.
speech()

playsound 软件包提供了一个简单的实用程序,可用于使用 Python 打开 -> 播放 -> 关闭 .mp3 文件。它非常方便,不会打开新的 window 或要求您强制解释器等待文件播放完毕。

playsound 将自己描述为 纯 Python、跨平台、单一功能模块,不依赖于播放声音,并且可以通过 运行.

pip install playsound

以下代码已经过测试,似乎非常适合您的用例。

from gtts import gTTS
from playsound import playsound

def speech():
   language = 'tr'
   myText = "Welcome"
   output = gTTS(text=myText, lang=language, slow=False)
   output.save("audio.mp3")
   playsound('audio.mp3')
   
speech()