中文语言的Google TTS客户端库参数设置

时间:2018-04-10 05:25:53

标签: google-text-to-speech

我尝试使用Google云端API进行texttospeech,以下是示例代码,它适用于英语;但是当我更改language_code =' en-US'到' zh-CN'并将输入文本设置为中文单词,显示错误: 400没有与TTS请求匹配的TTS语音。请更正语音选择参数,然后重试。

 Owner Execute Access Denied!
No policies granting access to resource.
Class: as-owner Resource: produsr User: ellen Access: execute

请教我使用中文时如何设置参数?

1 个答案:

答案 0 :(得分:1)

我使用gTTS包来解决这个问题,它适用于我,这里是代码:

def text_to_speech_gtts(res_ans):
    from gtts import gTTS
    volume=1.0
    music_file="ans01.mp3"
    tts = gTTS(text=res_ans, lang='zh-tw')
    tts.save(music_file)
    freq = 25000    # audio CD quality
    bitsize = -16    # unsigned 16 bit
    channels = 2     # 1 is mono, 2 is stereo
    buffer = 2048    # number of samples (experiment to get best sound)
    pg.mixer.init(freq, bitsize, channels, buffer)
    # volume value 0.0 to 1.0
    pg.mixer.music.set_volume(volume)
    clock = pg.time.Clock()
    try:
        pg.mixer.music.load(music_file)
    except pg.error:
        print("File {} not found! ({})".format(music_file, pg.get_error()))

    pg.mixer.music.play()
    while pg.mixer.music.get_busy():
        # check if playback has finished
        clock.tick(30) #
    pg.mixer.music.stop()
    pg.mixer.quit()