skip to Main Content

I keep getting this error when I create an instance of AudioPlayer but I don’t get it when I remove of instances of it from it from the code and when the check the plugins code I saw this method:

_JustAudioPlayer? _player;

@override
  Future<AudioPlayerPlatform> init(InitRequest request) async {
    if (_player != null) {
      throw PlatformException(
          code: "error",
          message:
              "just_audio_background supports only a single player instance");
    }
    _player = _JustAudioPlayer(
      initRequest: request,
    );
    return _player!;
  }

And that makes ZERO sense to me because what I’m seeing here is if an instance of AudioPlayer exists then throw said error, but I need the instance to play audio(unless I’m reading it wrong). Whenever there’s an instance of AudioPlayer, in the widget I’m using I get the error and then I can’t play anything but without just_audio_background, everything works fine. The error just halts any use of AudioPlayer through out the app; it throws the same error anytime I load a widget that uses AudioPlayer. I tried using audioservice but I’m having issues loading the song from network.

All the soltuions I’ve seen concerning this error message had a different and was fixed but not in this case.

2

Answers


  1. Chosen as BEST ANSWER

    I moved the instance of AudioPlayer to my main.dart instad of putting it in a widget and I stopped getting the error completely. Remember that there has to be one sinlge instance of AudioPlayer. So AudioPlayer player = AudioPlayer(); in main.dart and reference it from anywhere.


  2. In the docs there is line:

    It supports the simple use case where an app has a single AudioPlayer
    instance.

    So, this is expected behaviour.

    If you need a few instance of the audio player you could implements
    just_audio + audio_service(and maybe even audio_session).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search