【发布时间】:2015-06-17 19:37:40
【问题描述】:
我已经搜索了所有内容,但无法使其始终如一地工作。我想在应用处于后台或锁定屏幕且铃声关闭时收到远程推送通知时播放音频。
我遵循的步骤:
1) 将所需的背景模式设置为“应用程序播放音频”到 info.plist。
2) 在应用程序中:didFinishLaunchingWithOptions: a) 将音频会话类别设置为播放 b) 激活音频会话。 c) 使应用接收远程控制事件并成为第一响应者
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// https://developer.apple.com/library/ios/qa/qa1668/_index.html
// For playback to continue when the screen locks, or when the Ring/Silent switch is set to silent, use the AVAudioSessionCategoryPlayback
BOOL result = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&sessionError]; // TODO AVAudioSessionCategorySoloAmbient
if (!result && sessionError) {
NSLog(@"AppDelegate error setting session category. error %@", sessionError);
}
else {
NSLog(@"AppDelegate setting session category is successful");
}
result = [audioSession setActive:YES error:&sessionError];
if (!result && sessionError) {
NSLog(@"AppDelegate error activating audio session. error %@", sessionError);
}
else {
NSLog(@"AppDelegate setting session active is successful");
}
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
3) 在 applicationDidEnterBackground 中: a) 开始后台任务 b) 接收远程控制事件
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"AppDelegate applicationDidEnterBackground: called");
NSLog(@"AppDelegate applicationDidEnterBackground: is calling beginBackgroundTaskWithExpirationHandler:");
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
NSLog(@"AppDelegate applicationDidEnterBackground: is calling beginReceivingRemoteControlEvents");
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
4) 当远程通知进来时,播放音频文件
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
_currentItem = [AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"Audio" withExtension:@"wav"]];
_audioPlayer = [AVPlayer playerWithPlayerItem:_currentItem];
[_currentItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
[_audioPlayer addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
NSLog(@"playAudio: calling [_audioPlayer play] on audioPlayer %@", _audioPlayer);
[_audioPlayer play];
}
它有时会起作用,但并非总是如此。任何想法如何使这项工作始终如一?
【问题讨论】:
-
而不是在
application:didFinishLaunchingWithOptions:中设置音频会话和applicationDidEnterBackground中的remoteEvent 侦听器尝试仅在didReceiveRemoteNotification中设置它们。可能会发生会话被其他应用程序覆盖。 -
@Aanabidden 谢谢,我试过了,但它不会播放。我也只是在 didReceiveRemoteNotification 中设置了 audioSession 并将 beginReceivingRemoteControlEvents 和 beginBackgroundTaskWithExpirationHandler 留在了 didEnterBackground 中,它有时但并非总是有效。我无所适从。还有什么想法吗?
标签: ios background avaudioplayer avaudiosession