toastedDeli
toastedDeli

Reputation: 580

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. I sometimes get an unhandled DOM exception in my console. 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

Upvotes: 0

Views: 10505

Answers (2)

Orwellophile
Orwellophile

Reputation: 13953

Don't forget you can no longer autoplay unmuted videos without interaction in Chrome. See - https://developer.chrome.com/blog/autoplay/

This works fine for me:

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

Upvotes: 6

norbeq
norbeq

Reputation: 3076

Have you tried event Hls.Events.MANIFEST_PARSED?

Example:

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

Upvotes: 5

Related Questions