英文:
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(<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 () {
})
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 :
<video
id={uniqueKey}
muted={muted}
loop={loop}
height='100%'
width='100%'
playsInline
/>
I also tried add eventListener as and stopping video but it also does not work :
videoElement.addEventListener('canplay', () => {
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 -->
<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>
<!-- end snippet -->
Cheers!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论