Thu Dec 30 2021
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 on how to modify the "AppDelegate.swift" file to change the default behavior:
1import AVFoundation2import UIKit34@main5class AppDelegate: UIResponder, UIApplicationDelegate {67func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {89 var categoryError :NSError?10 var success: Bool11 do {12 // see https://developer.apple.com/documentation/avfoundation/avaudiosessioncategoryplayback13 // and https://developer.apple.com/documentation/avfoundation/avaudiosessionmodemovieplayback14 try AVAudioSession.sharedInstance().setCategory(.playback, mode: .moviePlayback, options: .duckOthers)15 success = true16 } catch let error as NSError {17 categoryError = error18 success = false19 }2021 if !success {22 print("AppDelegate Debug - Error setting AVAudioSession category. Because of this, there may be no sound. \(categoryError!)")23 }2425 // Override point for customization after application launch.26 return true27}2829// MARK: UISceneSession Lifecycle30func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {31 // Called when a new scene session is being created.32 // Use this method to select a configuration to create the new scene with.33 return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)34}3536func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {37 // Called when the user discards a scene session.38 // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.39 // Use this method to release any resources that were specific to the discarded scenes, as they will not return.40}4142}