florent37 / Flutter-AssetsAudioPlayer

Play simultaneously music/audio from assets/network/file directly from Flutter, compatible with android / ios / web / macos, displays notifications
https://pub.dartlang.org/packages/assets_audio_player
Apache License 2.0
752 stars 357 forks source link

Playing multiple audios at the same time. #765

Open noeljayson opened 1 year ago

noeljayson commented 1 year ago

Clicking on the next button multiple times it plays multiple audios at the same time

mt633 commented 1 year ago

I also noticed this issue. I'm not really a Kotlin programmer, but from what I understand the use of GlobalScope in the open method is the cause of the problem here. https://github.com/florent37/Flutter-AssetsAudioPlayer/blob/0fcc9c55a35c240ca209c24e601d75863281c6d8/android/src/main/kotlin/com/github/florent37/assets_audio_player/Player.kt#L184

That is not canceled if it's not finished before a new signal to open is sent and the result is two scopes running in parallel. From what I understand you could resolve it by creating your own CoroutineScope that you cancel if it's not finished before launching a new scope.

I've tried the following approach in open and it seems to work for me. You can check out the full code here.

 try {
    stop(pingListener = false)
    if (scope.isActive) {
        scope.cancel()
        scope = MainScope()
    }
} catch (t: Throwable) {
    print(t)
}

// Lines removed for readability

scope.launch(Dispatchers.Main) {

If anyone with more Kotlin experience is able to see a better approach for it, I'll gladly hear it!

KRTirtho commented 1 year ago

@mt633 your fork seem to be fix the issue. Please consider creating a PR? This is a major bug

mt633 commented 1 year ago

I would if this project seemed more alive. Haven't seen any response or active development from the project manager(s) in some time and I don't really want to take the time to create a PR for this if it'll never be merged.

kalismeras61 commented 1 year ago

@mt633 I'm not actively developing, but if you create a PR, I'll merge it.

mt633 commented 1 year ago

Thanks @kalismeras61, good to know. Then I'll try to find time to create a PR for this (and probably a few other PR for some other bugs/features as well).

akarapetsas commented 1 year ago

Thank you @mt633 for the support! Much appreciated

cuifengcn commented 1 year ago

realy a major bug