bluefireteam / audioplayers

A Flutter package to play multiple audio files simultaneously (Android/iOS/web/Linux/Windows/macOS)
https://pub.dartlang.org/packages/audioplayers
MIT License
2.01k stars 845 forks source link

Unable to play sound in android api < 23 #1344

Closed aideric closed 1 year ago

aideric commented 1 year ago

Checklist

Current bug behaviour

Unable to set source and play with audioPlayer (PlayerMode mediaPlayer) error throw on configAndPrepare in WrappedPlayer https://github.com/bluefireteam/audioplayers/blob/414d94da9b0de91bd0928af3468435cfc589e6f0/packages/audioplayers_android/android/src/main/kotlin/xyz/luan/audioplayers/player/WrappedPlayer.kt#L306-L307

Expected behaviour

Able to play sound and no error throw in the init part. Error should throw when the user use the setRate function in api < 23.

Steps to reproduce

  1. Install to example app in android device api < 23
  2. change mode to play instead of setSource
  3. Press Asset 1 - laser.wav
  4. See a crash

Logs

Unexpected error! java.lang.IllegalStateException: Changing the playback rate is only available for Android M/23+ or using LOW_LATENCY mode.
Full Logs ``` Unexpected error! java.lang.IllegalStateException: Changing the playback rate is only available for Android M/23+ or using LOW_LATENCY mode. at xyz.luan.audioplayers.player.MediaPlayerPlayer.setRate(MediaPlayerPlayer.kt:45) at xyz.luan.audioplayers.player.WrappedPlayer.configAndPrepare(WrappedPlayer.kt:309) at xyz.luan.audioplayers.player.WrappedPlayer.setSource(WrappedPlayer.kt:32) at xyz.luan.audioplayers.AudioplayersPlugin.handler(AudioplayersPlugin.kt:91) at xyz.luan.audioplayers.AudioplayersPlugin.access$handler(AudioplayersPlugin.kt:25) at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:41) at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:41) at xyz.luan.audioplayers.AudioplayersPlugin$safeCall$1.invokeSuspend(AudioplayersPlugin.kt:62) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42) at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664) [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(Unexpected error!, Changing the playback rate is only available for Android M/23+ or using LOW_LATENCY mode., java.lang.IllegalStateException: Changing the playback rate is only available for Android M/23+ or using LOW_LATENCY mode. at xyz.luan.audioplayers.player.MediaPlayerPlayer.setRate(MediaPlayerPlayer.kt:45) at xyz.luan.audioplayers.player.WrappedPlayer.configAndPrepare(WrappedPlayer.kt:309) at xyz.luan.audioplayers.player.WrappedPlayer.setSource(WrappedPlayer.kt:32) at xyz.luan.audioplayers.AudioplayersPlugin.handler(AudioplayersPlugin.kt:91) at xyz.luan.audioplayers.AudioplayersPlugin.access$handler(AudioplayersPlugin.kt:25) at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:41) at xyz.luan.audioplayers.AudioplayersPlugin$onAttachedToEngine$1$1.invoke(AudioplayersPlugin.kt:41) at xyz.luan.audioplayers.AudioplayersPlugin$safeCall$1.invokeSuspend(AudioplayersPlugin.kt:62) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42) at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664) , null) #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:653:7) #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:296:18) #2 AudioPlayer.play (package:audioplayers/src/audioplayer.dart:123:5) #3 _SourcesTabState.setSource (package:audioplayers_example/tabs/sources.dart:46:7) ``` Flutter doctor: ``` [✓] Flutter (Channel stable, 3.3.9, on macOS 13.0.1 22A400 darwin-arm, locale en-HK) • Flutter version 3.3.9 on channel stable at • Upstream repository https://github.com/flutter/flutter.git • Framework revision b8f7f1f986 (2 weeks ago), 2022-11-23 06:43:51 +0900 • Engine revision 8f2221fbef • Dart version 2.18.5 • DevTools version 2.15.0 ```

Environment information

Platform 1:

More information

we can change to configAndPrepare to

    private fun Player.configAndPrepare() {
        try {
            setRate(rate)
        }catch (e: Exception){ }
        setVolume(volume)
        setLooping(isLooping)
        prepare()
    }

or check the sdk version in the Player.configAndPrepare before calling setRate

With the "try catch", I can play sound on android 5.1.1

ignkarman commented 1 year ago

@aideric I think that setting Player to Low Latency mode will resolve this issue for you. It doesn't seem that the MediaPlayer supports variable playback rates.

player.setPlayerMode(PlayerMode.LowLatency)

Gustl22 commented 1 year ago

@aideric are you interested in working on a fix? We are currently on our capacities 😅

Ahmed-Elswerky commented 1 year ago

I'm having the same issue in android 5.1 any update please?