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

Trackend gets called multiple times #159

Closed gagansawhney closed 6 years ago

gagansawhney commented 7 years ago

In v 3.2 trackend gets called multiple times. This was fine till v3.0

veyDer commented 7 years ago

Having the same issue. My use case is to count the number of times a user played the whole track.

In the console I can see:

First Play

Playing track /assets/audio/ogg/test.ogg

Play ended

Finished playback Track finished {src: "/assets/audio/ogg/test.ogg", preload: "metadata", id: 0}

Second Play

Loading track /assets/audio/ogg/test.ogg Track finished {src: "/assets/audio/ogg/test.ogg", preload: "metadata", id: 0} Track finished {src: "/assets/audio/ogg/test.ogg", preload: "metadata", id: 0} Track finished {src: "/assets/audio/ogg/test.ogg", preload: "metadata", id: 0} Playing track /assets/audio/ogg/test.ogg

Play ended

Finished playback Track finished {src: "/assets/audio/ogg/test.ogg", preload: "metadata", id: 0}

Third Play

Loading track /assets/audio/ogg/test.ogg Track finished {src: "/assets/audio/ogg/test.ogg", preload: "metadata", id: 0} Track finished {src: "/assets/audio/ogg/test.ogg", preload: "metadata", id: 0} Track finished {src: "/assets/audio/ogg/test.ogg", preload: "metadata", id: 0} Playing track /assets/audio/ogg/test.ogg

veyDer commented 7 years ago

I've managed to workaround the issue by:

onTrackFinished(track: any) { let true_finished : any = Object.assign({},this._audioProvider.tracks[this._audioProvider.current]) if (true_finished._isLoading || true_finished._hasLoaded) return ... }

betatools commented 6 years ago

@veyDer I can't get your example to work. Can you explain further what you did?

Thx in advance

veyDer commented 6 years ago

This is the relevant part of my .ts file:

repetition_cnt: number = 3 my_is_playing = false

togglePlayingFlag() { this.my_is_playing = !this.my_is_playing } onTrackFinished(track: any) { let true_finished : any = Object.assign({},this._audioProvider.tracks[this._audioProvider.current]) if (true_finished._isLoading || true_finished._hasLoaded) return console.log('Track finished') if (!this.my_is_playing) return this.my_is_playing = false

this.repetition_cnt--
if (this.repetition_cnt == 0) {
  // the track has been played 3 times, do your stuff...
}

}

and here's the relevant part of my .html file: `

` Hope that helps.

betatools commented 6 years ago

Thank you for your help @veyDer

I'm not quite there yet. I can make the file play x times. After that it seems that the onFinish event is being called whenever I push a button (play button, next button etc). It's like the event is being fired on any change.

Are you using the code from npm or did you pull something from here?

arielfaur commented 6 years ago

@betatools Are you still struggling with this?

betatools commented 6 years ago

It works as expected the first time the audio file plays. When pressing play again, the onFinish method is being called right away and multiple times. I “fixed” it by setting a timer that prevents the logic in the onFinish event to run in the first few seconds of a play.

Now I can set a counter that works and makes it possible for the user to repeat a file x times

So to answer your question - I am not currently struggling with it, but it seems the onFinish event is weird.

I hope that made any sense 🙂

I have uploaded a screenshot here to explain the use of the repeat functionality

https://s18.postimg.org/epglo4u15/Image.png

arielfaur commented 6 years ago

Thanks, I will look into it!

arielfaur commented 6 years ago

@betatools I just pushed version 3.2.2 to npm which fixes this issue with the track end event being called multiple times. Can you give it a go and let me know? 😄

betatools commented 6 years ago

@arielfaur Sure. Thank you very much for fixing this. I’ll get back to you soon 🙂

rdai commented 6 years ago

I just want to confirm that 3.2.2 fixed this, for me. Thank you so much!