2016-04-14 132 views
3

我需要使用SSML在我的Alexa Skill中使用标签播放音频文件(按照Amazon的说明)。如何将SSML合并到Python中

问题是,我不知道如何在Python中使用SSML。我知道我可以在Java中使用它,但我想用Python构建我的技能。我已经看遍了所有,但还没有发现任何有效的Python脚本/程序中的SSML示例 - 有谁知道吗?

回答

0

这个问题有点含糊,但我设法弄清楚如何将SSML合并到Python脚本中。下面是播放一些音频的片段:

if 'Item' in intent['slots']: 
    chosen_item = intent['slots']['Item']['value'] 
    session_attributes = create_attributes(chosen_item) 

    speech_output = '<speak> Here is something to play' + \ 
    chosen_item + \ 
    '<audio src="https://s3.amazonaws.com/example/example.mp3" /> </speak>' 
+1

我试过了,但Alexa只是读出字符串的内容.... – Richard

+0

用户宝马已经指出了正确的答案。如果将'outputSpeech' JSON对象的'type'参数设置为'SSML'并且使用'ssml'而不是'text',则可以使用SSML标记(如[语音合成标记语言(SSML)参考文献] (https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/speech-synthesis-markup-language-ssml-reference))。 – abaumg

1

的SSML包蟒存在。

您可以通过点子

 


    $ pip install pyssml 
    or 
    $ pip3 install pyssml 

 

安装像下面这样的例子是低于

http://blog.naver.com/chandong83/221145083125 遗憾的链接。它是韩国。

 


    # -*- coding: utf-8 -*- 
    # for amazon 
    import re 
    import os 
    import sys 
    import time 
    from boto3 import client 
    from botocore.exceptions import BotoCoreError, ClientError 
    import vlc 
    from pyssml.PySSML import PySSML 


    # amazon service fuction 
    # if isSSML is True, SSML format 
    # else Text format 
    def aws_polly(text, isSSML = False): 
     voiceid = 'Joanna' 

     try: 
      polly = client("polly", region_name="ap-northeast-2") 

      if isSSML: 
       textType = 'ssml' 
      else: 
       textType = 'text' 

      response = polly.synthesize_speech(
        TextType=textType, 
        Text=text, 
        OutputFormat="mp3", 
        VoiceId=voiceid) 

      # get Audio Stream (mp3 format) 
      stream = response.get("AudioStream") 

      # save the audio Stream File 
      with open('aws_test_tts.mp3', 'wb') as f: 
       data = stream.read() 
       f.write(data) 


      # VLC play audio 
      # non block 
      p = vlc.MediaPlayer('./aws_test_tts.mp3') 
      p.play() 

     except (BotoCoreError, ClientError) as err: 
      print(str(err)) 


    if __name__ == '__main__': 
     # normal pyssml 
     #s = PySSML() 

     # amazon speech ssml 
     s = AmazonSpeech() 

     # normal 
     s.say('i am normal') 

     # speed is very slow 
     s.prosody({'rate':"x-slow"}, 'i am very slow') 

     # volume is very loud 
     s.prosody({'volume':'x-loud'}, 'my voice is very loud') 

     # take a one sec 
     s.pause('1s') 

     # pitch is very high 
     s.prosody({'pitch':'x-high'}, 'my tone is very high') 

     # amazone 
     s.whisper('i am whispering') 
     # print to convert to ssml format 
     print(s.ssml()) 

     # request aws polly and play 
     aws_polly(s.ssml(), True) 

     # Wait while playback. 
     time.sleep(50) 

 
相关问题