简体   繁体   English

如何让 Hls.js 在准备好后自动播放视频?

[英]How can I make Hls.js autoplay a video when its ready?

I've tried to video.play() on listeners to many of the possible events, but none of them work all the time.我尝试在许多可能事件的侦听器上使用video.play() ,但它们都不是一直有效。 I sometimes get an unhandled DOM exception in my console.我有时会在控制台中收到未处理的 DOM 异常。 Which event is the correct one to listen to, or how else can I achieve this functionality?哪个事件是正确的听,或者我怎样才能实现这个功能?

The events I've tried to listen to so far are到目前为止我尝试听的事件是

Hls.Events.LEVEL_LOADED
Hls.Events.FRAG_LOADED
Hls.Events.BUFFER_APPENDED

I'm simply adding listeners for these events like so:我只是为这些事件添加侦听器,如下所示:

Hls.on(EVENT, playVideo);

Hls.js: https://github.com/video-dev/hls.js hls.js: https://github.com/video-dev/hls.js

Have you tried event Hls.Events.MANIFEST_PARSED ? 您是否尝试过事件Hls.Events.MANIFEST_PARSED

Example: 例:

hls.on(Hls.Events.MANIFEST_PARSED,playVideo);

Don't forget you can no longer autoplay unmuted videos without interaction in Chrome.不要忘记,如果没有 Chrome 中的交互,您将无法再自动播放未静音的视频。 See - https://developer.chrome.com/blog/autoplay/见 - https://developer.chrome.com/blog/autoplay/

This works fine for me:这对我来说很好:

    hls.on(Hls.Events.MEDIA_ATTACHED, function () {
      video.muted = true;
      video.play();
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM