iOS app does not play audio while background
You need to set your app Capabilities Background Modes (Audio and AirPlay) and set your AVAudioSession category to AVAudioSessionCategoryPlayback and set it active.
Please Refer to this link below :
How to play audio in background with Swift?
iOS swift streaming app does not play music in background mode
Setting the audio background mode is correct, but I think you also need to set the audio category for the audio session.
Try adding this to your app delegate's didFinishLaunchingWithOptions
:
var activeError: NSError? = nil
AVAudioSession.sharedInstance().setActive(true, error: &activeError)
if let actError = activeError {
NSLog("Error setting audio active: \(actError.code)")
}
var categoryError: NSError? = nil
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: &categoryError)
if let catError = categoryError {
NSLog("Error setting audio category: \(catError.code)")
}
How to play audio in background with Swift?
You need to set your app Capabilities Background Modes (Audio and AirPlay) and set your AVAudioSession category to AVAudioSessionCategoryPlayback and set it active
From Xcode 11.4 • Swift 5.2
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])
print("Playback OK")
try AVAudioSession.sharedInstance().setActive(true)
print("Session is Active")
} catch {
print(error)
}
Related Topics
Swift Uiview Opacity Programmatically
How to Programmatically Open Apple Watch Companion App from iOS App
Customize Google Maps Blue Dot for Current Location
Adding an Skscene to a Uiviewcontroller
Ar Refernce Image Plane Was Not Position Properly in iOS Swift
Resize All Subview in Scrollviewdidzoom
Pass Extra Argument for UItapgesturerecognizer with Selector
Programmatically Change Splash Screen in iOS
How to Programmatically Wrap Png Texture Around Cube in Scenekit
How to Create Custom Album in Swift 3 iOS 10
Replacement for Arkit in iOS10
How to Make Collage of Images According to Different Shapes
No Such Module 'Fbsdkcorekit' Xcode 7.4
Box Shadow Spread Bug in Webkit in iOS 7 on Retina Ipads
iOS UIlabel Autoshrink So Word Doesn't Truncate to Two Lines
Data Repeat in UItableview When Scrolling