我试图制作一个文本到语音GUI程序,这是TTS的代码
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‘
发布于 2017-06-21 07:32:59
看上去你不会在加载文件后释放它。尝试使用上下文管理器,并将文件对象而不是文件传递给load方法:
...
with open('F:/tesst.mp3', 'rb') as file_object:
pygame.mixer.music.load(file_object)
mixer.music.play(0)
...一旦退出上下文管理器,资源将被释放。
发布于 2019-07-10 06:17:34
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')https://stackoverflow.com/questions/44669322
复制相似问题