flutterdarticecastjust-audioaudio-service

Flutter platform player already exists just audio


So happy with the just audio flutter package from Ryan Heise for our radio app. Best package I know. We have no problems with iOS, but on Android now some strange behavior, especially on Android 10. I'm also using the audio service and audio session packages to play the radio as background music. When the user hits play, no music is playing and error in the console:

Error: E/flutter ( 6721): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(Platform player 6f26b522-c6c3-4226-be22-80f9d15dfc2c already exists, null, null, null) E/flutter ( 6721): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:653:7) E/flutter ( 6721): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:296:18) E/flutter ( 6721): E/flutter ( 6721): #2 MethodChannelJustAudio.init (package:just_audio_platform_interface/method_channel_just_audio.dart:13:5) E/flutter ( 6721): E/flutter ( 6721): #3 AudioPlayer._setPlatformActive.setPlatform (package:just_audio/just_audio.dart:1255:13) E/flutter ( 6721):

Code in main.dart:
Future<void> main() async {
  // initialise audio_service with our audio_handler
  myAudioHandler = await AudioService.init(
    builder: () => MyAudioHandler(),
    config: const AudioServiceConfig(
      androidNotificationChannelId: 'com.ryanheise.myapp.channel.audio',
      androidNotificationChannelName: 'Audio playback',
      androidNotificationOngoing: true,
    ),
);

Code in audio_handler.dart:
Future<void> _loadRadio() async {
  final session = await AudioSession.instance;
  await session.configure(const AudioSessionConfiguration.music());

  // Try to load the radio from the source and catch any errors.
  try {
    await _player.setAudioSource(AudioSource.uri(
        Uri.parse([stream_address])));

  } catch (e) {
    print("Error loading audio source: $e");
  }
}

What does this error mean? And why is it only giving problems on some Android versions?

On Android 12 this is not happening all the time. On Android 10 this is happening a lot. We have several user complaints with the Android platform.


Solution

  • The error was coming from a myAudioHandler.stop command inside an internet connection checker function. When removed, everything worked again with all our Android users. This code was for testing purposes, and shouldn't be there. I hope this helps others with this error. Thanks for your help.