2017-05-27 24 views
1

我有一些代碼需要知道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() 

回答

1

要檢查是否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