【发布时间】:2012-05-12 20:09:08
【问题描述】:
使用 OpenFrameworks 为 iOS 设计一个生成音乐系统,我需要提供一种模式,让用户可以在以下情况下聆听应用程序生成的音乐:
- 设备已锁定
- 用户使用其他应用程序
一些应用程序,如 BLOOM 或闹钟,就是这样工作的,并向用户建议启用/禁用此功能的开关。
有什么建议吗?
【问题讨论】:
标签: ios audio background multitasking locked
使用 OpenFrameworks 为 iOS 设计一个生成音乐系统,我需要提供一种模式,让用户可以在以下情况下聆听应用程序生成的音乐:
一些应用程序,如 BLOOM 或闹钟,就是这样工作的,并向用户建议启用/禁用此功能的开关。
有什么建议吗?
【问题讨论】:
标签: ios audio background multitasking locked
播放背景音频
连续播放或录制音频的应用程序(即使应用程序在 在后台运行)可以注册以在 背景。您从背景模式部分启用音频支持 Xcode 项目中的 Capabilities 选项卡。 (您也可以启用 通过在音频中包含 UIBackgroundModes 键来支持此功能 应用程序的 Info.plist 文件中的值。)播放音频内容的应用程序 背景必须播放有声内容而不是静音。
Apple reference "Playing and Recording Background Audio"
Ensuring That Audio Continues When the Screen Locks
为了启用/禁用此功能,我找到了Activating and Deactivating Your Audio Session,我自己没有尝试过,但它看起来像你需要的。
【讨论】:
您需要在 plist 文件中进行一些更改。
即 1) 将必需的后台模式设置为应用播放音频
2) 设置应用程序不在后台运行为NO。
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
那么,你需要在 AppDelegate 中写这么多代码
现在,您可以在手机屏幕锁定或进入后台时轻松播放音频。
【讨论】:
在 xCode 项目设置和代码中进行以下更改。
步骤 1) 在 Xcode 的导航器中选择您的项目文件。然后,从 Capabilities 部分,打开 Background Modes 子部分。在为您提供背景模式列表后,勾选 Audio & Airplay 开关。
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
}
此音频播放会话将播放您的应用程序播放,即使应用程序处于后台或手机处于静音模式或设备已锁定。
【讨论】:
看看这个教程。它有一些后台服务示例
【讨论】:
您需要在 plist 文件中进行一些更改。
1) 将必需的后台模式设置为应用播放音频
2) 设置应用程序不在后台运行为NO。
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)")
}
});
【讨论】:
你也可以使用这个代码:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:NULL];
AVAudioSession *backgroundMusic = [AVAudioSession sharedInstance];
[backgroundMusic setCategory:AVAudioSessionCategoryPlayback error:NULL];
【讨论】: