Is it possible to play audio file or stream?

前提是你 提交于 2019-12-18 05:18:13

问题


Is it possible to play audio file or stream using actions-on-google-nodejs library?


回答1:


Using SSML you can return an audio clip up to 120s.

<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>

Edit

If you want to play audio the mp3 file (over 120s), you need to use 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',
      }),
    }));



回答2:


To add one more point to Nick's answer, you can also build a Media Response which will allow you to play a long audio file (I'm currently playing 50 mins album in my app). You can find it on Google's doc here.

A short example in Node.js could be:

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"]);

And then you can just do

app.ask(richResponse)

UPDATE:

As per a comment request, here is the JSON response sent by my app for a mediaResponse:

{
  "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\":{}}"
}


来源:https://stackoverflow.com/questions/48452906/is-it-possible-to-play-audio-file-or-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!