【问题标题】:How can I delete a sound when the sound is stopped?声音停止时如何删除声音?
【发布时间】:2021-10-05 00:31:29
【问题描述】:

我可以在使用 playsound 模块停止时删除音频文件吗?我写了一段代码,但我做不到:

from gtts import gTTS
import os
import playsound

def say(textg):
    tts = gTTS(text=textg, lang='en')
    tts.save('audio.mp3')
    playsound('audio.mp3')
    time.sleep(here, what i have to do?)
    os.remove('audio.mp3')

我使用的是 Python 2.7.15

【问题讨论】:

  • 看起来playsound 没有这样做的功能。你试过playsound(None)吗?您始终可以创建一个空的或无声的音频文件并播放它以“停止”声音。
  • 是否有其他模块具有执行此操作的功能?任何建议!
  • Pygame 可以启动和停止音频。花点时间了解 Pygame 的工作原理,然后看看 pygame.org/docs/ref/mixer.html
  • 这能回答你的问题吗? How to stop audio with playsound module?

标签: python python-2.7 python-playsound


【解决方案1】:

playsound 是模块的名称,你应该使用playsound.playsound(这是一个函数名)来代替:

from gtts import gTTS
import os
import playsound

def say(textg):
    tts=gTTS(text=textg,lang='en')
    tts.save('audio.mp3')
    playsound.playsound('audio.mp3')
    os.remove('audio.mp3')

调用playsound.playsound后,程序会一直等到结束。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多