iOS Background Audio Not Playing

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 Audio/Sound won't play in background with background mode active

There is a relevant note in the audio_service 0.18.0 README which can help here:

Note that the audio background mode permits an app to run in the background only for the purpose of playing audio. The OS may kill your process if it sits idly without playing audio, for example, by using a timer to sleep for a few seconds. If your app needs to pause for a few seconds between audio tracks, consider playing a silent audio track to create that effect rather than using an idle timer.

How to handle background audio playing while iOS device is locked or on another application?

Playing Background Audio

An app that plays or records audio continuously (even while the app is
running in the background) can register to perform those tasks in the
background. You enable audio support from the Background modes section
of the Capabilities tab in your Xcode project. (You can also enable
this support by including the UIBackgroundModes key with the audio
value in your app’s Info.plist file.) Apps that play audio content in
the background must play audible content and not silence.

Apple reference "Playing and Recording Background Audio"

Ensuring That Audio Continues When the Screen Locks

For enabling/disabling this feature I found Activating and Deactivating Your Audio Session, I haven't tried it myself, but it looks like what you need.

iOS: Start playing sound from background

At a very basic level there are three prerequisites your app should satisfy to play audio in the background:

  • Music must be playing
  • Setup ‘Background Modes’ for Audio in your Target Capabilities.

  • For basic playback, initialise your audio session, and set your audio session category to AVAudioSessionCategoryPlayback. If you don’t, you’ll get the default behaviour.

You should also configure your app to respond to

  • changes in audio output
  • audio session interruptions (e.g. phone call)
  • remote control events (via control centre, or the lock screen)

Check out this Swift example.

I cannot play music in the background (Xcode 9, Swift 4)

After spending a couple of days on this issue, I finally figured it out. In swift 4, I think we need to add a couple of lines of code to the appDelegate class under the project directory. Like this:

import UIKit
import AVFoundation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let session = AVAudioSession.sharedInstance()
do{
try session.setActive(true)
try session.setCategory(AVAudioSessionCategoryPlayback)
} catch{
print(error.localizedDescription)
}
return true
}

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}

Update: Swift 4.2


This helped also for me:


1. First step:

Sample Image


2. Second:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

let session = AVAudioSession.sharedInstance()
do{
try session.setActive(true)
try session.setCategory(.playback, mode: .default, options: .defaultToSpeaker)
} catch{
print(error.localizedDescription)
}
return true
}


Related Topics



Leave a reply



Submit