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):
VS (Mac):
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:
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
} catch _ {
return print("error")
}
Related Topics
Can Afnetworking Return Data Synchronously (Inside a Block)
In Ios13 the Status Bar Background Colour Is Different from the Navigation Bar in Large Text Mode
Enabling Auto Layout in iOS 6 While Remaining Backwards Compatible With iOS 5
Get Indexpath of Uitableviewcell on Click of Button from Cell
How to Make a Push Segue When a Uitableviewcell Is Selected
How to Make an Http Request + Basic Auth in Swift
Error Opening iOS Simulator with iOS 8 Beta Version: "Unable to Boot the iOS Simulator"
Blank Space at Top of Uitextview in iOS 10
How to Change the Device Orientation Programmatically in iOS 6
Table Header Views in Storyboards
How to Set the Width of a Cell in a Uitableview in Grouped Style
Sharing Data in Between Apps in Ios
How to Get the Color of a Pixel in a Uiimage With Swift
Wkwebview Equivalent for Uiwebview's Scalespagetofit
iOS 9: How to Change Volume Programmatically Without Showing System Sound Bar Popup