2

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

Ryan Heise
  • 2,273
  • 5
  • 14
Kitcc
  • 2,138
  • 4
  • 24
  • 42
  • I am also using the same package but I am not able to find an audio streaming position and complete audio duration... can you please help? – Chirag Chopra May 15 '20 at 12:50

2 Answers2

2

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)
}
Kitcc
  • 2,138
  • 4
  • 24
  • 42
  • I am also using the same package but I am not able to find an audio streaming position and complete audio duration... can you please help? – Chirag Chopra May 15 '20 at 12:50
  • Check the answer to this ticket, it contains what you are looking for : https://stackoverflow.com/questions/60165915/flutter-just-audio-playbackevent – Kitcc May 18 '20 at 09:41
  • Adding this code block isn't working for me, in silent mode sound is not playing as expected – Wilson Christian Sep 22 '21 at 10:18
2

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.

Ryan Heise
  • 2,273
  • 5
  • 14