我正在编写一个简单的python程序,该程序获取文本文件,然后使用IBM Watson文本到语音转换将其转换为音频,然后使用playsound等模块直接播放音频。
大多数教程都向您展示了如何将结果仅保存到文件中,而不是如何传递结果以便模块播放音频
from ibm_watson import TextToSpeechV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
authenticator = IAMAuthenticator('{apikey}')
text_to_speech = TextToSpeechV1(
authenticator=authenticator
)
text_to_speech.set_service_url('{url}')
with open('hello_world.wav', 'wb') as audio_file:
audio_file.write(
text_to_speech.synthesize(
'Hello world',
voice='en-US_AllisonVoice',
accept='audio/wav'
).get_result().content)
这不是我想要的,我希望能够在不保存的情况下播放音频,我该怎么做。
如果您开放外部库,则可以使用pip install python-vlc
为 python 安装vlc
绑定
并使用播放器方法直接从内容中播放音频,如下所示。
import vlc
from ibm_watson import TextToSpeechV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
authenticator = IAMAuthenticator('{apikey}')
text_to_speech = TextToSpeechV1(
authenticator=authenticator
)
text_to_speech.set_service_url('{url}')
#define VLC instance
instance = vlc.Instance('--input-repeat=-1', '--fullscreen')
#Define VLC player
player=instance.media_player_new()
#Define VLC media
media=instance.media_new(
text_to_speech.synthesize(
'Hello world',
voice='en-US_AllisonVoice',
accept='audio/wav').get_result().content)
#Set player media
player.set_media(media)
#Play the media
player.play()
vlc播放器的优点是您可以直接从URL(不仅仅是mp3(播放大多数媒体类型,还可以执行类似播放器的选项,例如
>>> play.pause() #pause play back
>>> player.play() #resume play back
>>> player.stop() #stop play back
*学分