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
I moved the instance of
AudioPlayer
to mymain.dart
instad of putting it in a widget and I stopped getting the error completely. Remember that there has to be one sinlge instance ofAudioPlayer
. SoAudioPlayer player = AudioPlayer();
inmain.dart
and reference it from anywhere.In the docs there is line:
…
…
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).