Flutter IOS just_audio not playing when phone in silent mode

Viewed 1517

I am developing an audio app. To play the various audio files I am using the just_audio package:

Just_Audio

It works fine on Android phones, but with iPhones when the phone is set to silent mode it is not playing any sound. Is there anyway to fix this that anyone has come across?

My code set up is very simple:

I declare the player:

final _player = AudioPlayer();

Set the url:

setAudioUrl(widget.mediaItem.itemUrl);

And play:

await _player.play();

Any help would be greatly appreciated.

Thanks

Carson

2 Answers

I added the following to my /ios/Runner/AppDelegate.swift file (inside didFinishLaunchingWithOptions) which fixed the issue - for anyone else interested.

do {
    if #available(iOS 10.0, *){
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault)
        try AVAudioSession.sharedInstance().setActive(true)
    }
} catch {
    print(error)
}

You can get the desired behaviour by setting your app's AVAudioSession category to playback via the audio_session package.

However, note that as of just_audio 0.4.0, this will actually be done for you by default. If you need to change the default, you can still do that via the audio_session package.

Related