gpt4 book ai didi

Javascript HTML5 视频事件无法在 Safari 上播放

转载 作者:行者123 更新时间:2023-12-01 02:06:44 25 4
gpt4 key购买 nike

我有一个包含 Javascript 的 HTML 页面,旨在允许视频(在本例中实际上只是音频内容)在任何浏览器上使用 HTTP Live Streaming 播放。在大多数情况下,它使用 hls.js,但对于 Apple 产品,我需要采取不同的做法,因为 Safari 具有 native HLS 支持。

完整页面复制如下,但重要的几行是:

else if (video.canPlayType('application/vnd.apple.mpegurl')) {
video.src = 'music.m3u8';
video.addEventListener('canplay', startPlaying);
//document.addEventListener('DOMContentLoaded', startPlaying);
}

应该发生的是,当 canplay 事件触发时,startPlaying() 函数被调用,这使得用户可以按下该按钮开始播放视频。然而,在我 friend 的 iPhone 8plus(iOS 11.3.1)上,这不起作用:没有任何按钮可见。相反,如果我注释掉 video.addEventListener() 行并将其替换为 document.addEventListener() 行,则一切正常:按钮可见他可以播放流。

有人能发现我做错了什么吗?可能是一个菜鸟错误,因为我对这种网络/脚本的东西不是很有经验,让我流鼻血......当然,我可以保留 DOM 加载方法,但这不是正确我宁愿是对的。

<!DOCTYPE html PUBLIC "-//Netscape Comm. Corp.//DTD HTML//EN">
<html>
<script src="hls.js/dist/hls.js"></script>

<head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head>
<body>
<video id="video"></video>
<button id="play" hidden>Loading</button>
<script>
'use strict';
var video = document.getElementById('video');
var playButton = document.getElementById('play');

function startPlaying() {
// For mobile browsers the start of playing has to
// be performed by a user action otherwise it will
// be ignored
playButton.addEventListener('click', function() {
video.play();
video.muted = false;
video.volume = 1;
playButton.innerHTML = "Playing";
});
playButton.hidden = false;
playButton.innerHTML = "Ready to play";
}

if (Hls.isSupported()) {
var hls = new Hls();
hls.loadSource('music.m3u8');
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED, startPlaying);
}
// hls.js is not supported on platforms that do not have Media Source Extensions (MSE) enabled.
// When the browser has built-in HLS support (check using `canPlayType`), we can provide an HLS manifest (i.e. .m3u8 URL) directly to the video element through the `src` property.
// This is using the built-in support of the plain video element, without using hls.js.
else if (video.canPlayType('application/vnd.apple.mpegurl')) {
video.src = 'music.m3u8';
video.addEventListener('canplay', startPlaying);
//document.addEventListener('DOMContentLoaded', startPlaying);
}
</script>
</body>
</html>

最佳答案

来自 other question 的调查,解决方法是等待事件 loadedmetadata,所以在我的例子中 video.addEventListener('loadedmetadata', startPlaying),因为这是您要进行的最后一个事件从 Safari 上的 HTML5 视频元素获取,除非您位于用户控制的白名单中。确认这适用于 iOS 11.3.1。

关于Javascript HTML5 视频事件无法在 Safari 上播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50051639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com