无法在 <video> 中停止加载首次从 Hls() 加载的 m3u8 视频。

huangapple go评论45阅读模式
英文:

Can not stop video in <video> while loading first time m3u8 from Hls()

问题

目前,我正在尝试在我的Web应用程序中使用HlsJS播放m3u8视频,但无论我加载视频时是否使用autoplay属性,它都会在第一次加载时开始播放。

以下是我的JavaScript代码:

const videoElement = document.getElementById(<unique-id>)

const hls = new Hls()
hls.loadSource(src)
hls.attachMedia(videoElement)

hls.on(Hls.Events.MEDIA_ATTACHED, function () {
    console.log('media attached')
})

hls.on(Hls.Events.MANIFEST_PARSED, function (event, data) {
})

hls.on(Hls.Events.MANIFEST_LOADED, function () {
})

请注意,我使用了此hls文件:https://cdnjs.cloudflare.com/ajax/libs/hls.js/1.4.3/hls.min.js

以下是HTML标签的代码:

<video
  id={uniqueKey}
  muted={muted}
  loop={loop}
  height='100%'
  width='100%'
  playsInline
/>

我还尝试添加事件侦听器以停止视频,但它也不起作用:

videoElement.addEventListener('canplay', () => {
    videoElement.pause()
})

在这里,我希望在加载视频时m3u8视频会停止播放。

英文:

Currently, I am trying use HlsJS to play m3u8 video in my web application but when I load video irrespective of autoplay attribute it starts playing when I load it first time.

Here is my javascript code:

const videoElement = document.getElementById(&lt;unique-id&gt;)


const hls = new Hls()
hls.loadSource(src)
hls.attachMedia(videoElement)

hls.on(Hls.Events.MEDIA_ATTACHED, function () {
    console.log(&#39;media attached&#39;)
})

hls.on(Hls.Events.MANIFEST_PARSED, function (event, data) {
})

hls.on(Hls.Events.MANIFEST_LOADED, function () {
})

Note here I have used this hls file : https://cdnjs.cloudflare.com/ajax/libs/hls.js/1.4.3/hls.min.js

Below code is of html tag :

&lt;video
  id={uniqueKey}
  muted={muted}
  loop={loop}
  height=&#39;100%&#39;
  width=&#39;100%&#39;
  playsInline
/&gt;

I also tried add eventListener as and stopping video but it also does not work :

videoElement.addEventListener(&#39;canplay&#39;, () =&gt; {
videoElement.pause()
})

Here, I am expecting m3u8 video gets stopped when I load the video.

答案1

得分: 1

你尝试过覆盖hlsOptions的autoStartLoad吗?

英文:

Did you try to override hlsOptions autoStartLoad?

答案2

得分: 0

我已添加海报到视频上并启用了控件,如果不需要,请删除它。

<video id="myVideo" muted loop height="50%" width="50%" playsinline controls></video>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<script>
    document.addEventListener('DOMContentLoaded', function () {
        const videoElement = document.getElementById('myVideo');
        const hls = new Hls();

        hls.loadSource('https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8');
        hls.attachMedia(videoElement);

        hls.on(Hls.Events.MEDIA_ATTACHED, function () {
            console.log('Media attached');
            extractPosterFromVideo(videoElement);
        });

        hls.on(Hls.Events.MANIFEST_PARSED, function () {
            videoElement.pause();
            console.log('Manifest parsed');
        });

        hls.on(Hls.Events.MANIFEST_LOADED, function () {
            videoElement.pause();
            console.log('Manifest loaded');
        });

        // Code is for Poster/Thumbnail of the video at 3 seconds scene

        function extractPosterFromVideo(video) {
            // Seek to the desired time in seconds
            const seekTime = 3;
            video.currentTime = seekTime;

            const canvas = document.createElement('canvas');
            const context = canvas.getContext('2d');

            // Set the canvas size to match the video dimensions
            canvas.width = video.videoWidth;
            canvas.height = video.videoHeight;
            video.addEventListener('seeked', function () {
                context.drawImage(video, 0, 0, canvas.width, canvas.height);
                const imageData = canvas.toDataURL();
                video.setAttribute('poster', imageData);
            });
        }

    });
</script>

Cheers!

英文:

Let me know if this is what you mean, I've added poster on the vide and enable the controls, just delete it if not needed.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;video id=&quot;myVideo&quot; muted loop height=&quot;50%&quot; width=&quot;50%&quot; playsinline controls&gt;&lt;/video&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/hls.js@latest&quot;&gt;&lt;/script&gt;
&lt;script&gt;
document.addEventListener(&#39;DOMContentLoaded&#39;, function () {
const videoElement = document.getElementById(&#39;myVideo&#39;);
const hls = new Hls();
hls.loadSource(&#39;https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8&#39;);
hls.attachMedia(videoElement);
hls.on(Hls.Events.MEDIA_ATTACHED, function () {
console.log(&#39;Media attached&#39;);
extractPosterFromVideo(videoElement);
});
hls.on(Hls.Events.MANIFEST_PARSED, function () {
videoElement.pause();
console.log(&#39;Manifest parsed&#39;);
});
hls.on(Hls.Events.MANIFEST_LOADED, function () {
videoElement.pause();
console.log(&#39;Manifest loaded&#39;);
});
//   Code is for Poster/Thumbnail of the video at 3 seconds scene
function extractPosterFromVideo(video) {
// Seek to the desired time in seconds
const seekTime = 3;
video.currentTime = seekTime;
const canvas = document.createElement(&#39;canvas&#39;);
const context = canvas.getContext(&#39;2d&#39;);
// Set the canvas size to match the video dimensions
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
video.addEventListener(&#39;seeked&#39;, function() {
context.drawImage(video, 0, 0, canvas.width, canvas.height);
const imageData = canvas.toDataURL();
video.setAttribute(&#39;poster&#39;, imageData);
});
}
});
&lt;/script&gt;

<!-- end snippet -->

Cheers!

huangapple
  • 本文由 发表于 2023年5月17日 16:31:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定