如何在不保存到文件的情况下收听 IBM Watson Text To Speech 结果 (python)

How to listen to IBM Watson Text To Speech result without saving to a file (python)

我正在编写一个简单的 python 程序,它获取一个文本文件,然后使用 IBM Watson Text To Speech 将其转换为音频,然后使用 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

*