代码之家  ›  专栏  ›  技术社区  ›  omega1

Alexa-音频播放器。播放问题在Echo Show Now Playing屏幕上显示内容

  •  7
  • omega1  · 技术社区  · 6 年前

    我在理解如何在音频播放器“正在播放”屏幕内的回声显示上显示图像时遇到问题。

    我正在播放一个音频文件,希望在“正在播放”屏幕上显示图像。我能得到的最接近的代码是下面的代码,它在音频开始前显示图像和标题,但随后立即消失,回声显示转到没有背景图像和元数据的“正在播放”屏幕。我觉得我已经很接近了,但是我不知道如何更新“正在播放”屏幕,而不是之前的屏幕。

    这是代码的一部分(按上述方式工作):

    var handlers = {
      'LaunchRequest': function() {
       this.emit('PlayStream');        
       },
    
    'PlayStream': function() {
     let builder = new Alexa.templateBuilders.BodyTemplate1Builder();
     let template = builder.setTitle('Test Title')
        .setBackgroundImage(makeImage('https://link_to_my_image.png'))
        .setTextContent(makePlainText('Test Text'))
        .build();
    
     this.response.speak('OK.').
    
     audioPlayerPlay(
        'REPLACE_ALL', 
        stream.url, 
        stream.url, 
        null, 
        0)
        .renderTemplate(template);
    
     this.emit(':responseReady');  
     }
    

    我一直在看这一页 https://developer.amazon.com/docs/custom-skills/audioplayer-interface-reference.html 但无法理解如何将页面上的结构转换为我的代码。我假设,从页面上的代码来看:

    {
      "type": "AudioPlayer.Play",
      "playBehavior": "valid playBehavior value such as ENQUEUE",
      "audioItem": {
        "stream": {
          "url": "https://url-of-the-stream-to-play",
          "token": "opaque token representing this stream",
          "expectedPreviousToken": "opaque token representing the previous stream",
          "offsetInMilliseconds": 0
        },
        "metadata": {
          "title": "title of the track to display",
          "subtitle": "subtitle of the track to display",
          "art": {
            "sources": [
              {
                "url": "https://url-of-the-album-art-image.png"
              }
            ]
          },
          "backgroundImage": {
            "sources": [
              {
                "url": "https://url-of-the-background-image.png"
              }
            ]
          }
        }
      }
    }
    

    我需要得到这个部分:

    "metadata": {
              "title": "title of the track to display",
              "subtitle": "subtitle of the track to display",
              "art": {
                "sources": [
                  {
                    "url": "https://url-of-the-album-art-image.png"
                  }
                ]
              },
    

    进入我的代码块:

    audioPlayerPlay(
            'REPLACE_ALL', 
            streamInfo.url, 
            streamInfo.url, 
            null, 
            0)
            .renderTemplate(template);
    

    (可能会失去 .renderTemplate(template); 部分原因是它只会在“正在播放”屏幕加载之前短暂闪烁。

    关于如何实现这个目标有什么想法吗?

    更新:

    我在index.js中添加了以下内容:

    var metadata = { 
        title: "title of the track to display",
        subtitle: "subtitle of the track to display",
        art: { 
            sources: {
                url: "https://url-of-the-album-art-image.png"
                } 
            }
        };
    

    并对音频播放器进行了如下修改:

    audioPlayerPlay(
        'REPLACE_ALL', 
        stream.url, 
        stream.url, 
        null, 
        0,
        metadata)
        .renderTemplate(template);
    

    并修改responseBuilder.js,如下所示:

    audioPlayerPlay(behavior, url, token, expectedPreviousToken, offsetInMilliseconds, metadata) {
        const audioPlayerDirective = {
            type : DIRECTIVE_TYPES.AUDIOPLAYER.PLAY,
            playBehavior: behavior,
            audioItem: {
                stream: {
                    url: url,
                    token: token,
                    expectedPreviousToken: expectedPreviousToken,
                    offsetInMilliseconds: offsetInMilliseconds,   
                    metadata : metadata 
                }
            }
        };
    
        this._addDirective(audioPlayerDirective);
        return this;
    }
    

    但我还是没有在“正在播放”屏幕上显示任何内容。

    2 回复  |  直到 6 年前
        1
  •  5
  •   omega1    6 年前

    由于某些原因,echo show没有实时更新,需要重新启动才能显示元数据变量中传递的内容,这就是为什么我没有看到任何结果。

    简单地传递一个变量就可以了。我只需要找出为什么内容会卡在“正在播放”屏幕上,需要重新启动才能工作。

    var "metadata": {
          "title": "title of the track to display",
          "subtitle": "subtitle of the track to display",
          "art": {
            "sources": [
              {
                "url": "https://url-of-the-album-art-image.png"
              }
            ]
          },
    
        2
  •  4
  •   omega1    6 年前

    只需定义如下的元数据。把它作为第六个参数传给AudioPlayerPlay;

    "metadata": {
              "title": "title of the track to display",
              "subtitle": "subtitle of the track to display",
              "art": {
                "sources": [
                  {
                    "url": "https://url-of-the-album-art-image.png"
                  }
                ]
              },
    
    audioPlayerPlay(
            'REPLACE_ALL', 
            streamInfo.url, 
            streamInfo.url, 
            null, 
            0,metadata)
    

    另外,要使其正常工作,您必须修改将要压缩并上载到lambda的节点模块。

    台阶- 转到node_modules\alexa sdk\lib并在其中打开responsebuilder文件。修改代码如下-

    audioPlayerPlay(behavior, url, token, expectedPreviousToken, offsetInMilliseconds, **metadata**) {
            const audioPlayerDirective = {
                type : DIRECTIVE_TYPES.AUDIOPLAYER.PLAY,
                playBehavior: behavior,
                audioItem: {
                    stream: {
                        url: url,
                        token: token,
                        expectedPreviousToken: expectedPreviousToken,
                        offsetInMilliseconds: offsetInMilliseconds
                    },
                    **metadata : metadata**
                }
            };
    
            this._addDirective(audioPlayerDirective);
            return this;
        }
    

    P.S.-只有在使用Alexa SDK版本1时才需要修改节点模块。