首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pygame.mixer.quit()不工作

pygame.mixer.quit()不工作
EN

Stack Overflow用户
提问于 2017-06-21 07:21:44
回答 2查看 587关注 0票数 0

我试图制作一个文本到语音GUI程序,这是TTS的代码

代码语言:javascript
复制
b = a.get()

blabla = b
tts = gTTS(text=blabla, lang='en-us')
try :
    tts.save("F:/tesst.mp3")
except :
    pass
pygame.init()
pygame.display.set_mode((2, 1))
try:
    pygame.mixer.music.load("F:/tesst.mp3")
except :
    pass
mixer.music.play(0)

clock = pygame.time.Clock()
clock.tick(10)
while pygame.mixer.music.get_busy():
    pygame.event.poll()
    clock.tick(10)
mixer.music.set_endevent()
mixer.quit()
os.remove("F:/tesst.mp3")

我得到错误声明文件已经被另一个程序使用,所以我不能递归地运行程序。这是错误

PermissionError: WinError 32进程无法访问文件,因为另一个进程正在使用它:'F:/tesst.mp3‘

EN

回答 2

Stack Overflow用户

发布于 2017-06-21 07:32:59

看上去你不会在加载文件后释放它。尝试使用上下文管理器,并将文件对象而不是文件传递给load方法:

代码语言:javascript
复制
...

with open('F:/tesst.mp3', 'rb') as file_object:
    pygame.mixer.music.load(file_object)

mixer.music.play(0)

...

一旦退出上下文管理器,资源将被释放。

票数 0
EN

Stack Overflow用户

发布于 2019-07-10 06:17:34

代码语言:javascript
复制
import boto3
import pygame
#from pygame import mixer
import os


profile_name = 'AWS_Account_Name'
SESSION = boto3.session.Session(profile_name=profile_name)
# Create an S3 client
polly_client = SESSION.client('polly')

while 1:
    response = polly_client.synthesize_speech(VoiceId='Joanna',
                    OutputFormat='mp3', 
                    Text = 'This is the test result that we need to play from the python')
    print('text to speech')
    file = open('speech.mp3', 'wb')
    file.write(response['AudioStream'].read())
    file.close()

    pygame.mixer.init()
    #pygame.mixer.music.load('speech.mp3')
    #pygame.mixer.music.play()

    with open('speech.mp3', 'rb') as file_object:
        pygame.mixer.music.load(file_object)
        pygame.mixer.music.play()
        while pygame.mixer.music.get_busy() == True:
            pass
    #pygame.mixer.music.play(0)
    pygame.mixer.quit()
    pygame.quit()
    os.remove('speech.mp3')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44669322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档