当Android屏幕锁定时,如何使html 5音频继续HLS音频播放?

时间:2019-03-28 15:56:59

标签: javascript android html5-audio

我正在使用html 5音频在Android的Chrome浏览器中播放HLS音频。

如何让Chrome在锁定流上或在浏览器选项卡处于非活动状态时继续播放HLS流?

在iOS上,当更改浏览器选项卡或锁定屏幕时,流将继续播放,但会在Android上停止音频。

      <source
        src="http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8"
        type="application/x-mpegURL"
      />
    </audio>

我什至添加了mediaSession信息以尝试并帮助它。

    var audio = document.getElementById("audio");
    if ("mediaSession" in navigator) {
      audio.onplay = function() {
        navigator.mediaSession.metadata = new MediaMetadata({
          title: "TEST",
          artist: "ARTIST"
        });
      };
    }

1 个答案:

答案 0 :(得分:0)

Android将以本地方式播放hls流,但是如果您希望在锁定屏幕上播放hls流,请使用hls.js

let audio = document.getElementById("hls-audio");
let source = "http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8"

if (Hls.isSupported()) {
  console.log("hello hls.js!");

  let hls = new Hls();
  hls.attachMedia(audio);
  hls.on(Hls.Events.MEDIA_ATTACHED, () => {
    console.log("video and hls.js are now bound together !");
    hls.loadSource(source);
  });
}
audio.onplay = () => {
  console.log("we can track events the same way with hls.js")
};