Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to play sound in android api < 23 #1344

Closed
2 tasks done
aideric opened this issue Dec 9, 2022 · 3 comments · Fixed by #1390
Closed
2 tasks done

Unable to play sound in android api < 23 #1344

aideric opened this issue Dec 9, 2022 · 3 comments · Fixed by #1390
Labels
bug platform-android Affects the android platform

Comments

@aideric
Copy link
Contributor

aideric commented Dec 9, 2022

Checklist

  • I read the troubleshooting guide before raising this issue
  • I made sure that the issue I am raising doesn't already exist

Current bug behaviour

Unable to set source and play with audioPlayer (PlayerMode mediaPlayer)
error throw on configAndPrepare in WrappedPlayer

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)
<asynchronous suspension>
#2      AudioPlayer.play (package:audioplayers/src/audioplayer.dart:123:5)
<asynchronous suspension>
#3      _SourcesTabState.setSource (package:audioplayers_example/tabs/sources.dart:46:7)
<asynchronous suspension>

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

  • audioplayers version: ^1.1.1

Platform 1:

  • OS name and version: android 5.1.1, API 22
  • Device: physical device
  • build mode: debug release
  • error peculiarities (optional):

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

@aideric aideric added the bug label Dec 9, 2022
@aideric aideric changed the title Unable to play sound in android < 23 Unable to play sound in android api < 23 Dec 9, 2022
@ignkarman
Copy link

@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 Gustl22 added the platform-android Affects the android platform label Jan 11, 2023
@Gustl22
Copy link
Collaborator

Gustl22 commented Jan 11, 2023

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

@Ahmed-Elswerky
Copy link

Ahmed-Elswerky commented Jan 16, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug platform-android Affects the android platform
Projects
None yet
4 participants