播放mp3后,Pyglet不会退出 [英] Pyglet won't quit after playing mp3

查看:278
本文介绍了播放mp3后,Pyglet不会退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was looking for a solution to play mp3 files in python and many stackoverflow answers (to other questions) seemed to recommend pyglet. I am writing a program that takes a piece of text, breaks it into individual words and then downloads mp3s of those words (if they aren't already downloaded) using gTTs and plays them.

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应用程序,以便我的代码可以继续执行?

However I find that after playing the files pyglet won't let my program progress. How can I exit the pyglet app after playback has finished, so that my code can progress?

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

Alternatively is there some other way that I can play mp3 files in python?

推荐答案

这很重要,因为app.run()是一个永无休止的循环,可以使GL上下文保持活动状态.解决此问题的方法只有一种,那就是创建自己的继承Pyglet属性的类.

This is souly because app.run() is a never-ending loop in order to keep the GL context alive. There's only one way around this and that is to create your own class that inherits the Pyglet properties.

我将为您提供示例代码,如果您有任何疑问,请随时提出.

I'll give you a sample code and if you have any questions feel free to ask away.

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.

Now this is just the most basic example of how to load audio sources and play them. You press Enter and that triggers beep.mp3.

通常,您还希望将函数连接到

Normally, you'd also want to hook a function to self.player.eos() that does something when you run out of sources.

还请注意,如果尚未播放,它只会调用play()一次.这些是您要尊重的属性.

Also note that it only calls play() once if it's not already playing. These are attributes you want to honor.

Ah和 Escape 退出应用程序.

Ah and Escape exits the application.

这篇关于播放mp3后,Pyglet不会退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆