2

I am using audioplayers (4.0.1) to play some short audio. The function runs ok as I can hear the audio gets played, but I see those warning messages in my console.

I am super new to Flutter, so my question is, should I care about this message, if so, how should I handle this unhandled exception?

I don't know in dart how to clearly ask to run a function on the ui thread.

  AudioPlayer? _player;
  void _play(number) async {
    _player?.dispose();
    final player = _player = AudioPlayer();
    print("trying to play number $number");
    player.play(AssetSource('audio/$number.wav'));
  }

...

 itemBuilder: (context, index){
   _play(index+1);
   return Colum(...)
}
I/flutter (32755): trying to play number 3
V/MediaPlayer(32755): resetDrmState:  mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
V/MediaPlayer(32755): cleanDrmObj: mDrmObj=null mDrmSessionId=null
V/MediaPlayer(32755): resetDrmState:  mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
V/MediaPlayer(32755): cleanDrmObj: mDrmObj=null mDrmSessionId=null
E/flutter (32755): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(AndroidAudioError, Methods marked with @UiThread must be executed on the main thread. Current thread: DefaultDispatcher-worker-1, java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: DefaultDispatcher-worker-1
E/flutter (32755):  at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:1415)
E/flutter (32755):  at io.flutter.embedding.engine.FlutterJNI.dispatchEmptyPlatformMessage(FlutterJNI.java:1015)
E/flutter (32755):  at io.flutter.embedding.engine.dart.DartMessenger.send(DartMessenger.java:280)
E/flutter (32755):  at io.flutter.embedding.engine.dart.DartExecutor$DefaultBinaryMessenger.send(DartExecutor.java:452)
E/flutter (32755):  at io.flutter.embedding.engine.dart.DartExecutor.send(DartExecutor.java:212)
E/flutter (32755):  at io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler$EventSinkImplementation.endOfStream(EventChannel.java:270)
E/flutter (32755):  at xyz.luan.audioplayers.EventHandler.endOfStream(AudioplayersPlugin.kt:354)
E/flutter (32755):  at xyz.luan.audioplayers.player.WrappedPlayer.dispose(WrappedPlayer.kt:356)
E/flutter (32755):  at xyz.luan.audioplayers.AudioplayersPlugin.handler(AudioplayersPlugin.kt:197)
E/flutter (32755):  at xyz.luan.audioplayers.AudioplayersPlugin.access$handler(AudioplayersPlugin.kt:29)
E/flutter (32755):  at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:50)
E/flutter (32755):  at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:50)
E/flutter (32755):  at xyz.luan.audioplayers.AudioplayersPlugin$safeCall$1.invokeSuspend(AudioplayersPlugin.kt:74)
E/flutter (32755):  at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
E/flutter (32755):  at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
E/flutter (32755):  at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
E/flutter (32755):  at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
E/flutter (32755):  at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
E/flutter (32755):  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
E/flutter (32755):  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
E/flutter (32755):  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
E/flutter (32755): , null)
E/flutter (32755): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:653:7)
E/flutter (32755): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:315:18)
E/flutter (32755): <asynchronous suspension>
E/flutter (32755): #2      AudioPlayer.dispose (package:audioplayers/src/audioplayer.dart:353:5)
E/flutter (32755): <asynchronous suspension>
Kaidi G
  • 43
  • 4

2 Answers2

0

It seems it was a bug and they fixed it two days ago: https://github.com/bluefireteam/audioplayers/issues/1481

Don't know when will they release that fix.

jglatre
  • 911
  • 1
  • 12
  • 15
0

I was also facing a PlatformException error, only with some Android devices, but not all. My solution was to change to the just_audio package:

https://pub.dev/packages/just_audio

It also supports url, assets, files and streams sources and it fixed my problem.