【问题标题】:Pyglet won't quit after playing mp3Pyglet 播放 mp3 后不会退出
【发布时间】:2016-08-12 18:10:28
【问题描述】:

我一直在寻找在 python 中播放 mp3 文件的解决方案,并且许多 stackoverflow 答案(对其他问题)似乎推荐 pyglet。我正在编写一个程序,它将一段文本分解成单个单词,然后使用 gTT 下载这些单词的 mp3(如果尚未下载)并播放它们。

from pyglet import media, app, clock
from gtts import gTTS
import os
import time
from num2words import num2words

cwd = os.getcwd()
beep = media.load('beep.mp3', streaming = False)

def get_mp3(text):
    player = media.Player()
    lowertext = text.lower()    
    words = lowertext.split()    
    player.queue(beep)
    for word in words:
        save_path = cwd + '\\tts_downloads\\{}.mp3'.format(word)
        if os.path.isfile(save_path) == False:
            tts = gTTS(word, 'en-us')
            tts.save(save_path)
        mp3 = media.load(save_path)
        player.queue(mp3)
    player.queue(beep)
    player.play()
    app.run()

但是我发现在播放文件后 pyglet 不会让我的程序继续进行。播放完成后如何退出 pyglet 应用程序,以便我的代码可以继续?

还有其他方法可以让我在 python 中播放 mp3 文件吗?

【问题讨论】:

    标签: python mp3 pyglet


    【解决方案1】:

    这是灵魂,因为app.run() 是一个永无止境的循环,以保持 GL 上下文活跃。只有一种方法可以解决这个问题,那就是创建您自己的继承 Pyglet 属性的类。

    我会给你一个示例代码,如果你有任何问题,请随时提问。

    import pyglet
    from pyglet.gl import *
    
    # Optional audio outputs (Linux examples):
    # pyglet.options['audio'] = ('alsa', 'openal', 'silent')
    key = pyglet.window.key
    
    class main(pyglet.window.Window):
        def __init__ (self):
            super(main, self).__init__(800, 800, fullscreen = False)
            self.x, self.y = 0, 0
    
            self.bg = pyglet.sprite.Sprite(pyglet.image.load('background.jpg'))
            self.sprites = {}
            self.player = pyglet.media.Player()
            self.alive = 1
    
        def on_draw(self):
            self.render()
    
        def on_close(self):
            self.alive = 0
    
        def on_key_press(self, symbol, modifiers):
            # Do something when a key is pressed?
            # Pause the audio for instance?
            # use `if symbol == key.SPACE: ...`
    
            # This is just an example of how you could load the audio.
            # You could also do a standard input() call and enter a string
            # on the command line.
            if symbol == key.ENTER:
                self.player.queue(media.load('beep.mp3', streaming = False))
                if nog self.player.playing:
                    self.player.play()
            if symbol == key.ESC: # [ESC]
                self.alive = 0
    
        def render(self):
            self.clear()
            self.bg.draw()
    
            # self.sprites is a dictionary where you store sprites
            # to be rendered, if you have any.
            for sprite_name, sprite in self.sprites.items():
                sprite.draw()
    
            self.flip()
    
        def run(self):
            while self.alive == 1:
                self.render()
    
                # -----------> This is key <----------
                # This is what replaces pyglet.app.run()
                # but is required for the GUI to not freeze
                #
                event = self.dispatch_events()
            self.player.delete() # Free resources. (Not really needed but as an example)
    
    x = main()
    x.run()
    

    现在这只是如何加载和播放音频源的最基本示例。您按 Enter 并触发 beep.mp3

    通常,您还希望将一个函数挂钩到 self.player.eos(),以便在您用完源时执行某些操作。

    另外请注意,如果它还没有播放,它只会调用一次play()。这些是你想要尊重的属性。

    啊,Escape 退出应用程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多