音频反馈在前景和背景
问题描述:
当我的应用程序在前景和背景中时,我必须提供音频反馈。但是当应用程序进入后台时,音频反馈不会被听到。在info.plist中,我设置了背景模式,以便应用程序播放音频或使用AirPlay播放音频/视频,并使用以下内容,但是当应用程序进入背景并且未听到音频时,不会调用audioPlayerDidFinishPlaying委托。音频反馈在前景和背景
AVAudioPlayer *sound = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
sound.delegate = self;
///Fixed the issue No audible feedback when main audio is silent
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
// This is necessary if you want to play a sequence of songs, otherwise your app will be
// killed after the first one finishes.
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[soundQueue addObject:sound];
答
您需要对plist文件进行几处更改。
1)设置所需的背景模式到App播放音频
2)设置在应用背景不运行为YES。
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
然后,你需要在AppDelegate中
写这些太多的代码现在,您可以轻松地运行,而手机屏幕锁定或背景音乐。
它适用于我。 :)
的更多信息,请参见Playing Audio in background mode和Audio Session Programming Guide
可能duplictate http://stackoverflow.com/questions/10429204/how-to-handle-background-audio-playing-while-ios-device-is -locked - 或上另一 – iCoder