iOS Swift Streaming App Does Not Play Music in Background Mode

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)
}

Sample Image



Related Topics



Leave a reply



Submit