How to Handle Background Audio Playing While iOS Device Is Locked or on Another Application

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 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?

How to keep audio playing when screen dims and locks?

You need to register your app for background audio. See: https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/registering-applications-to-run-in-background

Applications can be registered by setting the Required Background Modes property in the application's Info.plist. An application can register in as many categories as it requires

So open your Info.plist file and select the following options:

VS (PC):

Sample Image

VS (Mac):

Sample Image

Initiate Download while iOS Device is Locked or App is in Background

You should request the system to let you run in the background while you are downloading the audiofile. You can do so by using UIApplication's beginBackgroundTaskWithExpirationHandler: method. Once you download the file, you can get the AVAudioPlayer to play your file.

Swift: Keep playing sounds when the device is locked

swift 2+ solution:

audio

do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
} catch _ {
return print("error")
}


Related Topics



Leave a reply



Submit