是否可以播放音频文件或流?

时间:2018-01-25 22:25:05

标签: actions-on-google dialogflow google-home

是否可以使用actions-on-google-nodejs库播放音频文件或流?

2 个答案:

答案 0 :(得分:5)

使用SSML,您可以返回最多120秒的音频剪辑。

<speak>
  <audio src="https://actions.google.com/sounds/v1/animals/cat_purr_close.ogg">
    <desc>a cat purring</desc>
    PURR (sound didn't load)
  </audio>
</speak>

修改

如果你想播放mp3文件的音频(超过120秒),你需要使用 Media Responses

if (!conv.surface.capabilities.has('actions.capability.MEDIA_RESPONSE_AUDIO')) {
      conv.ask('Sorry, this device does not support audio playback.');
      return;
    }
    conv.ask(new MediaObject({
      name: 'Jazz in Paris',
      url: 'https://storage.googleapis.com/automotive-media/Jazz_In_Paris.mp3',
      description: 'A funky Jazz tune',
      icon: new Image({
        url: 'https://storage.googleapis.com/automotive-media/album_art.jpg',
        alt: 'Ocean view',
      }),
    }));

答案 1 :(得分:4)

要再向Nick的答案添加一个点,您还可以制作媒体响应,这样您就可以播放长音频文件(我目前在我的应用中播放50分钟的专辑)。 您可以在Google的文档here上找到它。

Node.js中的一个简短示例可能是:

const richResponse = app.buildRichResponse()
 .addSimpleResponse("Here's song one.")
  .addMediaResponse(app.buildMediaResponse()
  .addMediaObjects([
    app.buildMediaObject("Song One", "https://....mp3")
      .setDescription("Song One with description and large image.") // Optional
      .setImage("https://....jpg", app.Media.ImageType.LARGE)
        // Optional. Use app.Media.ImageType.ICON if displaying icon.
  ])
)
.addSuggestions(["other songs"]);

然后你可以做到

app.ask(richResponse)

<强>更新

根据评论请求,以下是我的应用为mediaResponse发送的JSON响应:

{
  "conversationToken": "[\"_actions_on_google\"]",
  "expectUserResponse": true,
  "expectedInputs": [
    {
      "inputPrompt": {
        "richInitialPrompt": {
          "items": [
            {
              "simpleResponse": {
                "textToSpeech": "Here is my favorite album."
              }
            },
            {
              "mediaResponse": {
                "mediaType": "AUDIO",
                "mediaObjects": [
                  {
                    "name": my_name,
                    "description": my_descr,
                    "largeImage": {
                      "url": my_url
                    },
                    "contentUrl": my_contentURL
                  }
                ]
              }
            }
          ],
          "suggestions": [
            {
              "title": my_suggestion
            }
          ]
        }
      },
      "possibleIntents": [
        {
          "intent": "assistant.intent.action.TEXT"
        }
      ]
    }
  ],
  "responseMetadata": {
    "status": {
      "message": "Success (200)"
    },
    "queryMatchInfo": {
      "queryMatched": true,
      "intent": "0a3c14f8-87ca-47e7-a211-4e0a8968e3c5",
      "parameterNames": [
        my_param_name
      ]
    }
  },
  "userStorage": "{\"data\":{}}"
}