当 iOS 设备被锁定或在其他应用程序上时如何处理背景音频播放?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10429204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle background audio playing while iOS device is locked or on another application?
提问by gluon
Designing a generative music system for iOS, using OpenFrameworks, I'd need to provide a mode in which the user could listen the music produced by the application when:
为 iOS 设计一个生成音乐系统,使用 OpenFrameworks,我需要提供一种模式,在这种模式下,用户可以在以下情况下聆听应用程序生成的音乐:
- the device is locked
- the user uses another application
- 设备被锁定
- 用户使用另一个应用程序
Some applications like BLOOM, or alarm clock, works like that and propose to users a switch to enable/disable this feature.
像 BLOOM 或闹钟这样的一些应用程序就是这样工作的,并向用户建议启用/禁用此功能的开关。
Any tips for that ?
任何提示?
回答by DanSkeel
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.
连续播放或录制音频(即使应用程序在后台运行)的应用程序可以注册以在后台执行这些任务。您可以从 Xcode 项目中功能选项卡的背景模式部分启用音频支持。(您也可以通过在应用程序的 Info.plist 文件中包含带有音频值的 UIBackgroundModes 键来启用此支持。)在后台播放音频内容的应用程序必须播放可听内容而不是静音。
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.
为了启用/禁用此功能,我发现Activating and Deactivation Your Audio Session,我自己没有尝试过,但它看起来像您需要的。
回答by dhaval rupani
You need to make couple of changes in plist file.
您需要在 plist 文件中进行一些更改。
i.e. 1) Set Required background mode to App plays audio
即 1) 将所需的后台模式设置为 App 播放音频
2) set Application does not run in background to NO.
2)将应用程序不在后台运行设置为否。
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
Then, you need to write these much code in AppDelegate
然后,你需要在 AppDelegate 中写这么多代码
Now, you can easily run audio while phone screen locks or goes in background.
现在,您可以在手机屏幕锁定或进入后台时轻松运行音频。
回答by Himanshu Mahajan
Make the following changes in xCode project settings as well as in code.
在 xCode 项目设置和代码中进行以下更改。
step 1) Select your project file in the Navigator of Xcode. Then, from the Capabilities section, switch on the Background Modes subsection. After the list of background modes is given to you, tick on the Audio & Airplay switch.
步骤 1) 在 Xcode 的导航器中选择您的项目文件。然后,从 Capabilities 部分,打开 Background Modes 子部分。在给您提供背景模式列表后,勾选音频和 Airplay 开关。
stp 2) Use following swift code, basically you need to set audio session for your app.
stp 2) 使用以下 swift 代码,基本上您需要为您的应用程序设置音频会话。
var audioPlayer : AVAudioPlayer!
@IBAction func playButtonClicked(sender : AnyObject){
let dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(dispatchQueue, {
if let data = NSData(contentsOfFile: self.audioFilePath())
{
do{
let session = AVAudioSession.sharedInstance()
try session.setCategory(AVAudioSessionCategoryPlayback)
try session.setActive(true)
self.audioPlayer = try AVAudioPlayer(data: data)
//self.audioPlayer.delegate = self
self.audioPlayer.prepareToPlay()
self.audioPlayer.play()
}
catch{
print("\(error)")
}
}
});
}
func audioFilePath() -> String{
let filePath = NSBundle.mainBundle().pathForResource("mySong", ofType: "mp3")!
return filePath
}
This audio playback session will be play your application playback, even if app is in background or phone is in silent mode or device is locked.
此音频播放会话将播放您的应用程序播放,即使应用程序在后台或手机处于静音模式或设备被锁定。
回答by hmlasnk
Look at on this tutorial. It has some of background services example
看看这个教程。它有一些后台服务示例
回答by Varun Naharia
You need to make couple of changes in plist file.
您需要在 plist 文件中进行一些更改。
1) Set Required background mode to App plays audio
1) 将所需的后台模式设置为 App 播放音频
2) set Application does not run in background to NO.
2)将应用程序不在后台运行设置为否。
let dispatchQueue = DispatchQueue.global()
dispatchQueue.async(execute: {
do{
let session = AVAudioSession.sharedInstance()
try session.setCategory(AVAudioSessionCategoryPlayback)
try session.setActive(true)
self.isObjectAllocate = true
if self.isPlayed == false{
self.playSound(soundName: "http://radio.zahraun.com:8000/live.m3u")
self.isPlayed = true
self.btnPlayAudio.setImage(#imageLiteral(resourceName: "pause") , for: .normal)
}else{
self.audioPlayer.pause()
self.isPlayed = false
self.btnPlayAudio.setImage(#imageLiteral(resourceName: "audioPlay"), for: .normal)
}
}
catch{
print("\(error)")
}
});
回答by Evgenii Melnik
Also you can use this code:
您也可以使用此代码:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:NULL];
AVAudioSession *backgroundMusic = [AVAudioSession sharedInstance];
[backgroundMusic setCategory:AVAudioSessionCategoryPlayback error:NULL];