从javascript / html播放音频时设置自定义元数据(Control Centrer iOS)

时间:2017-08-31 14:45:03

标签: javascript html ios audio

我需要在html页面中从标记<audio>播放音频时更改插图(在控制中心中)。在Android上它由navigator.mediaSession.metadata工作(参见下面的代码),但在iOS上它不起作用。你有什么想法解决它吗?

  <script>
    if ('mediaSession' in navigator) {
        navigator.mediaSession.metadata = new MediaMetadata({
              title: 'Unforgettable',
              artist: 'Nat King Cole',
              album: 'The Ultimate Collection (Remastered)',
              artwork: [
                      { src: 'https://dummyimage.com/96x96',   sizes: '96x96',   type: 'image/png' },
                      { src: 'https://dummyimage.com/128x128', sizes: '128x128', type: 'image/png' },
                      { src: 'https://dummyimage.com/192x192', sizes: '192x192', type: 'image/png' },
                      { src: 'https://dummyimage.com/256x256', sizes: '256x256', type: 'image/png' },
                      { src: 'https://dummyimage.com/384x384', sizes: '384x384', type: 'image/png' },
                      { src: 'https://dummyimage.com/512x512', sizes: '512x512', type: 'image/png' },
                    ]
            });
        navigator.mediaSession.setActionHandler('play', function() {
          document.querySelector('audio').play();
        });
        navigator.mediaSession.setActionHandler('pause', function() {
          document.querySelector('audio').pause();
        });
    }
  </script>

1 个答案:

答案 0 :(得分:0)

目前(2018.04.07)iOS浏览器不支持。 Chrome for Android只是支持媒体会话API的浏览器。 https://developer.mozilla.org/en-US/docs/Web/API/Media_Session_API