arielfaur / ionic-audio

An audio player for Ionic 3 and Angular 4. Works with HTML 5 audio or native audio using Cordova Media plugin.
http://arielfaur.github.io/ionic-audio/2.0/index.html
MIT License
321 stars 163 forks source link

Proper way to stop audio track #146

Open azmathmoosa opened 7 years ago

azmathmoosa commented 7 years ago

Hi, thanks for the great work.

I am a noob to the whole angular/npm ecosystem so go easy on me. I have a single audio player that will be playing only one track at a time. When a user selects a different url, I then change the url of the audio component and it works.

The problem is, I can see in my chrome dev tools that the old track is still being downloaded in the background even though it is no longer needed!. This is hogging my BW and I would like to fix it. I think I am using this wrong but please guide me.

This is my code when the track is changed:

console.log("Track Changed!");`

document.execCommand("Stop", false);

this.audioComponent.pause();

this.audioProvider.stop();

this.audioComponent = null;

//while (this.audioProvider.tracks.length > 0)
//  this.audioProvider.tracks.pop();
this.audioComponent = new AudioTrackComponent(this.audioProvider);

this.audioComponent.track = this.currentTrack;

this.audioComponent.ngOnInit();

//set handler to inform parent track finished playing
this.audioComponent.onFinish.subscribe((event) => this.loadingStatus.emit(2));

`

arielfaur commented 7 years ago

There are two ways to interact with an audio track. Either by using the AudioTrack component or by means of the provider. In both cases, if you call stop() it should unload the current audio. Is that not happening?

azmathmoosa commented 7 years ago

It continues to download the old track for some time.

In the image, track 1 is being played but on switching to 11, it continues to download 1 for about 20-30 seconds. Same for track 11 to 27. But notice 27 to 28, if the track is switched before it loads (green region represents waiting time), then it doesn't cancel the old request! It downloads and plays both tracks. I hear both tracks being played but I can only control the last one!

image

Thanks for your time 👍

arielfaur commented 7 years ago

Hi @azmathmoosa Thanks for the detailed report. I assume you are testing on the browser with the default HTML Audio. I am afraid that is the way HTML Audio works. I am not sure there is a way to cancel download completely, or is there one?

arielfaur commented 6 years ago

162