gpt4 book ai didi

javascript - 使用 HLS Stream 时向 plyr 添加质量选择器

转载 作者:行者123 更新时间:2023-12-04 12:24:26 26 4
gpt4 key购买 nike

我正在使用 plyr作为 HTML5 视频标签的包装器并使用 Hls.js 流式传输我的 .m3u8 视频。

我在 plyr 上解决了很多问题以启用质量选择器,并遇到了多个 PR,这些 PR 有这个问题但被关闭说实现已合并,直到我解决了这个 PR它说它仍然是开放的,但是在评论中有一个自定义实现可以确保它可以工作。我正在本地尝试该实现,以检查我们是否可以添加质量选择器,但似乎我缺少某些东西/或实现dosent工作。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HLS Demo</title>
<link rel="stylesheet" href="https://cdn.plyr.io/3.5.10/plyr.css" />
<style>
body {
max-width: 1024px;
}
</style>
</head>
<body>
<video preload="none" id="player" autoplay controls crossorigin></video>
<script src="https://cdn.plyr.io/3.5.10/plyr.js"></script>
<script src="https://cdn.jsdelivr.net/hls.js/latest/hls.js"></script>
<script>
(function () {
var video = document.querySelector('#player');
var playerOptions= {
quality: {
default: '720',
options: ['720']
}
};
var player;
player = new Plyr(video,playerOptions);
if (Hls.isSupported()) {
var hls = new Hls();
hls.loadSource('https://content.jwplatform.com/manifests/vM7nH0Kl.m3u8');
//hls.loadSource('https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8');
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED,function(event,data) {

// uncomment to see data here
// console.log('levels', hls.levels); we get data here but not able to see in settings .

playerOptions.quality = {
default: hls.levels[hls.levels.length - 1].height,
options: hls.levels.map((level) => level.height),
forced: true,
// Manage quality changes
onChange: (quality) => {
console.log('changes',quality);
hls.levels.forEach((level, levelIndex) => {
if (level.height === quality) {
hls.currentLevel = levelIndex;
}
});
}
};
});
}

// Start HLS load on play event
player.on('play', () => hls.startLoad());

// Handle HLS quality changes
player.on('qualitychange', () => {
console.log('changed');
if (player.currentTime !== 0) {
hls.startLoad();
}
});
})();

</script>
</body>
</html>


The above snippet works please run , but also if you uncomment the line in HLS Manifest you will see we get data in levels and also pass the data to player options but it dosent come up in settings.How can we add a quality selector to plyr when using Hls stream .

最佳答案

我在 github [1] 上对此发表了长篇评论。

工作示例:https://codepen.io/datlife/pen/dyGoEXo

解决这个问题的主要思想是:

  • 配置 Plyr 选项 正确地允许切换发生。
  • 让 HLS 执行质量切换,而不是 Plyr。因此,我们只需要 video 中的单个源标签。标签。
  • <video>
    <source
    type="application/x-mpegURL"
    <!-- contain all the stream -->
    src="https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8">
    </video>

    [1] https://github.com/sampotts/plyr/issues/1741#issuecomment-640293554

    关于javascript - 使用 HLS Stream 时向 plyr 添加质量选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61300439/

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