2016-04-14 128 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) 

 
相關問題