How to let audio play when the iOS device is in Silent mode?
By default, audio only plays if the iOS device is unmuted (mute side switcher on iOS devices).
In order for audio to work by default, the app must declare that audio playback as a core feature of the app, and thus it should not be muted in “Silent Mode”.
This can be done by configuring the AVAudioSessionCategoryPlayback
category: Apple Developer Documentation
You can choose from several audio session categories and settings to customize your app's audio behavior.
Bellow an example how to modify the AppDelegate.application(_:didFinishLaunchingWithOptions:)
file to change the default behavior:
import AVFoundation
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
do {
// see https://developer.apple.com/documentation/avfoundation/avaudiosessioncategoryplayback
// and https://developer.apple.com/documentation/avfoundation/avaudiosessionmodemovieplayback
try AVAudioSession.sharedInstance()
.setCategory(.playback, mode: .moviePlayback, options: .duckOthers)
} catch {
print("AppDelegate Debug - Error setting AVAudioSession category. Because of this, there may be no sound. \(error)")
}
return true
}
}
Updated 12 months ago