Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Future already completed #147

Open
tejainece opened this issue Oct 31, 2024 · 6 comments
Open

fix: Future already completed #147

tejainece opened this issue Oct 31, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@tejainece
Copy link

tejainece commented Oct 31, 2024

Happens on Mac after recovering from suspend.

[ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: Bad state: Future already completed
#0      _AsyncCompleter.complete (dart:async/future_impl.dart:84:31)
#1      SoLoud._initializeNativeCallbacks.<anonymous closure> (package:flutter_soloud/src/soloud.dart:423:18)
#2      _RootZone.runUnaryGuarded (dart:async/zone.dart:1609:10)
#3      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:366:11)
#4      _DelayedData.perform (dart:async/stream_impl.dart:542:14)
#5      _PendingEvents.handleNext (dart:async/stream_impl.dart:647:11)
#6      _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:618:7)
#7      _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#8      _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
@tejainece tejainece added the bug Something isn't working label Oct 31, 2024
@JoMinSeo
Copy link

JoMinSeo commented Nov 5, 2024

I am currently experiencing this issue as well.

@alnitak
Copy link
Owner

alnitak commented Nov 5, 2024

Could you please tell me the steps to reproduce this, that would help to find a solution?

Before suspending, do some sounds were playing? Were there sounds loaded (and not disposed)?

Also I think you are on an old version, could you also try the latest?

@Taormina
Copy link
Contributor

Taormina commented Nov 5, 2024

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: Future already completed
#0      _AsyncCompleter.complete (dart:async/future_impl.dart:43:31)
#1      SoLoud._initializeNativeCallbacks.<anonymous closure> (package:flutter_soloud/src/soloud.dart:509:61)
#2      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#3      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:365:11)
#4      _DelayedData.perform (dart:async/stream_impl.dart:541:14)
#5      _PendingEvents.handleNext (dart:async/stream_impl.dart:646:11)
#6      _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:617:7)
#7      _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#8      _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

I'm on 2.1.7 and saw this on 2.1.6 briefly. It happened around the start of the audio, but I'm not sure what the exact trigger is. The audio sounds right, so I'm not sure that this is causing any actual problems, but I did also notice this in the logs. Still trying to better gather data to help reproduce

@Taormina
Copy link
Contributor

Taormina commented Nov 5, 2024

I can reproduce it by triggering a sound repeatedly for the first time, so I'm assuming that the await has not been processed from the first invocation of the sound?

Hrm, I can probably load that properly before letting the player perform the actions repeatedly that cause the sound....

@alnitak
Copy link
Owner

alnitak commented Nov 5, 2024

Thank you @Taormina,

so I could presume that the suspend activity is not really related to this issue.

Your exception comes from package:flutter_soloud/src/soloud.dart:509:61 which is the completer triggered when a file is loaded. @tejainece could you please tell me on which version you are?

@Taormina
Copy link
Contributor

Taormina commented Nov 5, 2024

I can confirm that loading all the unique sounds once at the beginning before user input is allowed both prevents the delay in audio, and prevents this from happening.

Below is an example that works. If the toSet() and the await on getSource are removed, because there are a few duplicate files in sfxFiles list at the moment, it would will always reproduce this issue.

  Future<void> loadAssets() async {
    // ....
    final soundFiles = (bgmFiles + sfxFiles).toSet();

    for (final sound in soundFiles) {
      await getSource(sound);
    }
    
    // ....
  }


  Future<AudioSource> getSource(String filename) async {
    if (!sounds.containsKey(filename)) {
      sounds[filename] =
          await SoLoud.instance.loadAsset('assets/audio/$filename');
    }
    return sounds[filename]!;
  }

I happen to be storing the AudioSources in a map for later reference, but just calling loadAsset once before attempting to play the sound multiple times is the actual fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants