2013-11-24 104 views
2

我在确定如何在后台为我的iOS应用程序播放音频时遇到困难。我有一个NSTimer,并希望在达到5分钟时播放声音,即使在背景中也是如此。我已经启用了音频背景模式,但不确定如何完成我的想法。即使在后台播放应用程序的音频

回答

8

在功能选择以下内容:

enter image description here

在你的plist确保它说:“应用程序在后台不运行”是NO。

enter image description here

然后添加以下到您的appDelegate.m文件。

NSError *sessionError = nil; 
NSError *activationError = nil; 
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&sessionError]; 
[[AVAudioSession sharedInstance] setActive: YES error: &activationError]; 

在下面的方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    return YES; 
} 
+0

会在哪里我我想要的声音补充的吗?另外,我想让NSTimer保持在后台,以便该应用知道何时播放声音。我已经设置了定时器并正在工作。 – PanicDev

+0

在您的viewcontroller实现文件中的任何位置添加它。如果你按照上面的说明,你的NSSTimer将在后台运行。 – 4GetFullOf

+0

Apple官方文档:https://developer.apple.com/library/ios/qa/qa1668/_index.html – Dunc

相关问题