如何检查混音器系统是否在Pygame中初始化?

问题描述:

我有一些代码需要知道Pygame中的混音器系统是否已初始化,因此它可以知道何时退出它,因为目前Pygame似乎没有正确退出。我有一个Python中的文本到语音转换程序,目前我正试图在每个操作系统上工作,就像以前它依赖于Windows Media Player一样。我试图用Pygame来达到这个目的,但是它在第二次使用Pygame后没有正确关闭。当它第一次加载.mp3文件时,它将成功退出Pygame并允许程序删除该文件,但如果用户选择再次尝试并再次进行文本到语音转换,它将重新初始化Pygame,写入该文件,打开然后播放该文件。文件完成播放后,它将尝试退出Pygame,但Pygame无法正常关闭,并且程序无法删除.mp3文件,因为它当前正在使用中。如何检查混音器系统是否在Pygame中初始化?

import os 
import time 
import sys 
import getpass 
import pip 
from contextlib import contextmanager 


my_file = "Text To Speech.mp3" 
username = getpass.getuser() 


@contextmanager 
def suppress_output(): 

    with open(os.devnull, "w") as devnull: 
     old_stdout = sys.stdout 
     sys.stdout = devnull 
     try: 
      yield 
     finally: 
      sys.stdout = old_stdout 


def check_and_remove_file(): 

    if os.path.isfile(my_file): 
     os.remove(my_file) 


def input_for_tts(message): 

    try: 

     tts = gTTS(text = input(message)) 
     tts.save('Text To Speech.mp3') 
     audio = MP3(my_file) 
     audio_length = audio.info.length 
     pygame.mixer.init() 
     pygame.mixer.music.load(my_file) 
     pygame.mixer.music.play() 
     time.sleep((audio_length) + 0.5) 
     pygame.mixer.music.stop() 
     pygame.mixer.quit() 
     pygame.quit() 
     check_and_remove_file() 

    except KeyboardInterrupt: 

     check_and_remove_file() 
     print("\nGoodbye!") 
     sys.exit() 


with suppress_output(): 

    pkgs = ['mutagen', 'gTTS', 'pygame'] 
    for package in pkgs: 
     if package not in pip.get_installed_distributions(): 
      pip.main(['install', package]) 


import pygame 
from pygame.locals import * 
from gtts import gTTS 
from mutagen.mp3 import MP3 


check_and_remove_file() 


input_for_tts("Hello there " + username + ". This program is\nused to output the user's input as speech.\nPlease input something for the program to say: ") 


while True: 

    try: 

     answer = input("\nDo you want to repeat? (Y/N) ").strip().lower() 
     if answer in ["n", "no", "nah", "nay", "course not"] or "no " in answer or "nah " in answer or "nay " in answer or "course not " in answer: 
      check_and_remove_file() 
      sys.exit() 
     elif answer in ["y", "yes", "yeah", "course", "ye", "yea", "yh"] or "yes " in answer or "yeah " in answer or "course " in answer or "ye " in answer or "yea " in answer or "yh " in answer: 
      input_for_tts("\nPlease input something for the program to say: ") 
     else: 
      print("\nSorry, I didn't understand that. Please try again with either Y or N.") 

    except KeyboardInterrupt: 

     check_and_remove_file() 
     print("\nGoodbye!") 
     sys.exit() 

要检查是否pygame.mixer被初始化,所有你需要做的是pygame.mixer.get_init()将返回当前正在播放的音频的数据,除非是未初始化的,在这种情况下,它会返回None

来源:https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.get_init

+0

不过,我宁愿只是声明了一个标志为它被初始化或者不这样我就可以知道,如果音频设备被插入或没有(如果您尝试初始化混频器时没有音频输出已启用,则会引发pygame.error异常)。 – Frogboxe

+0

谢谢,这非常有帮助。我在初始化时简单地添加了一个'try-except',这样如果它引发了'pygame.error',它会告诉用户代码无法完成并退出。我需要'pygame.mixer.get_init()',以便代码知道在尝试删除它所使用的文件时是否需要关闭退出'pygame.mixer'。 – Gameskiller01