Skip to content

Commit

Permalink
fix: promise issue after channel switch #29
Browse files Browse the repository at this point in the history
this commit closes #29
  • Loading branch information
4gray committed May 18, 2021
1 parent 6b2c615 commit d1f194a
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class HtmlVideoPlayerComponent implements OnChanges, OnDestroy {
console.log('... switching channel to ', channel.name, url);
this.hls.loadSource(url);
this.hls.attachMedia(this.videoPlayer.nativeElement);
this.videoPlayer.nativeElement.play();
this.handlePlayOperation();
} else if (
this.videoPlayer.nativeElement.canPlayType(
'application/vnd.apple.mpegurl'
Expand All @@ -56,12 +56,27 @@ export class HtmlVideoPlayerComponent implements OnChanges, OnDestroy {
this.videoPlayer.nativeElement.addEventListener(
'loadedmetadata',
() => {
this.videoPlayer.nativeElement.play();
this.handlePlayOperation();
}
);
}
}

/**
* Handles promise based play operation
*/
handlePlayOperation(): void {
const playPromise = this.videoPlayer.nativeElement.play();

if (playPromise !== undefined) {
playPromise
.then(() => {
// Automatic playback started!
})
.catch((error) => console.error(error));
}
}

/**
* Destroy hls instance on component destroy
*/
Expand Down

0 comments on commit d1f194a

Please sign in to comment.