Skip to content

Commit 8de9aec

Browse files
authored
Merge pull request #104 from ghenry22/fix-web-audio-playback-with-transcoding
update the web audio init, release and options. Resolves stalling audio playback when transcoding
2 parents 460cc50 + 963a9d9 commit 8de9aec

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/web.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { validateTrack, validateTracks } from './utils';
2222
declare var Hls: any;
2323

2424
export class PlaylistWeb extends WebPlugin implements PlaylistPlugin {
25-
protected audio: HTMLVideoElement | undefined;
25+
protected audio: HTMLAudioElement | undefined;
2626
protected playlistItems: AudioTrack[] = [];
2727
protected loop = false;
2828
protected options: AudioPlayerOptions = {};
@@ -99,6 +99,15 @@ export class PlaylistWeb extends WebPlugin implements PlaylistPlugin {
9999
return Promise.resolve();
100100
}
101101

102+
async create(): Promise<void> {
103+
this.audio = document.createElement('audio');
104+
this.audio.crossOrigin = 'anonymous';
105+
this.audio.preload = 'metadata';
106+
this.audio.controls = true;
107+
this.audio.autoplay = false;
108+
return Promise.resolve();
109+
}
110+
102111
removeItem(options: RemoveItemOptions): Promise<void> {
103112
this.playlistItems.forEach((item, index) => {
104113
if (options.index && options.index === index) {
@@ -380,17 +389,10 @@ export class PlaylistWeb extends WebPlugin implements PlaylistPlugin {
380389
let wasPlaying = false;
381390
if (this.audio) {
382391
wasPlaying = !this.audio.paused;
383-
this.audio.pause();
384-
this.audio.src = '';
385-
this.audio.removeAttribute('src');
386-
this.audio.load();
387-
}
388-
this.audio = document.createElement('video');
389-
if (wasPlaying || forceAutoplay) {
390-
this.audio.addEventListener('canplay', () => {
391-
this.play();
392-
});
392+
await this.release();
393393
}
394+
await this.create();
395+
394396
this.currentTrack = item;
395397
if (item.assetUrl.includes('.m3u8')) {
396398
await this.loadHlsJs();
@@ -407,15 +409,23 @@ export class PlaylistWeb extends WebPlugin implements PlaylistPlugin {
407409

408410
//this.registerHlsListeners(hls, position);
409411
} else {
410-
this.audio.src = item.assetUrl;
412+
this.audio!.src = item.assetUrl;
411413
}
412414

413415
await this.registerHtmlListeners(position);
414416

415417
this.updateStatus(RmxAudioStatusMessage.RMXSTATUS_TRACK_CHANGED, {
416418
currentItem: item
417419
})
420+
421+
if (wasPlaying || forceAutoplay) {
422+
//this.play();
423+
this.audio!.addEventListener('canplay', () => {
424+
this.play();
425+
});
426+
}
418427
}
428+
419429
protected updateStatus(msgType: RmxAudioStatusMessage, value: any, trackId?: string) {
420430
this.notifyListeners('status', {
421431
action: 'status',

0 commit comments

Comments
 (0)