Fry
Fry

Reputation: 6275

Play sounds with AVQueuePlayer even if iPhone is muted

I'm using AVQueuePlayer to play sounds (a little App for kids to listen illustrated books), but if iPhone is muted the audio isn't audible.

There is a way to force audio even if iPhone is muted? I think to YouTube where audio is audible or Instagram when you have to pop up the volume to listen stories.

Thanks.

Upvotes: 1

Views: 1050

Answers (3)

will
will

Reputation: 141

Updated solution for IOS 15 and above:

func enablePlayingWhileRingerIsSetToSilent() {
    guard let _ = try? AVAudioSession.sharedInstance().setCategory(
        AVAudioSession.Category.playback,
        mode: AVAudioSession.Mode.default,
        options: []
    ) else { return }
}

Key differences:

  • AVAudioSessionCategoryPlayback got change to AVAudioSession.Category.playback
  • AVAudioSessionModeDefault tranformed to AVAudioSession.Mode.default

If you wish to be able to continue playing the sound in the background just add the audio to the UIBackgroundModes in info.plist as stated here

Upvotes: 0

Naqeeb
Naqeeb

Reputation: 1411

By default AVPlayer is dependent on ringer position if the ringer is in silent mode you can't listen to audio. if you want to play audio even the ringer is in silent position then set setCategory as AVAudioSessionCategoryPlayback before playing video.

try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault, options: [])

Upvotes: 9

Duveral
Duveral

Reputation: 335

I don't think you can't overrule the hardware button (neither you should), a better approach would be to check if the device is silent and show an alert to the user in that case, BUT, as the below library says:

There is no native iOS API to detect if the mute switch is enabled/disabled on a device.

The general principle to check if the device is muted is to play a short sound and detect the length it took to play. From this length we can determine if it was muted or not.

The library that may help you.

Upvotes: -2

Related Questions