如何在Boto3中使用AWS Polly进行暂停

问题描述

我的代码如下:

import boto3
polly_client = boto3.Session().client('polly')    

response = polly_client.synthesize_speech(
    VoiceId='Joanna',OutputFormat='mp3',Text = sentence
)
audio = response['AudioStream']

我尝试使用以下句子:

sentence = '''<speak><s>Mary had a little lamb</s> <s>Whose fleece was white as sNow</s>And everywhere that Mary went,the lamb was sure to go.</speak>'''

但是生成的音频没有暂停,它只是读出文本。

解决方法

这会为短语“ Hello world”生成一个音频文件,两个单词之间的间隔为2秒:

import boto3
polly_client = boto3.Session().client('polly')

sentence = '''<speak>Hello <break time='2000ms'/> World</speak>'''

response = polly_client.synthesize_speech(
    VoiceId='Joanna',OutputFormat='mp3',TextType='ssml',Text=sentence
)

file = open('speech.mp3','wb')
file.write(response['AudioStream'].read())
file.close()

有关更多详细信息,请参见Using SSML下的aws文档小节。