Ios 在后台应用程序不工作时播放音频

Ios 在后台应用程序不工作时播放音频,ios,background,avaudioplayer,avaudiosession,Ios,Background,Avaudioplayer,Avaudiosession,我已经搜索了所有地方,但无法使其始终有效。我想在应用程序处于后台或锁定屏幕且振铃器关闭时,在收到远程推送通知时播放音频 我遵循的步骤是: 1) 将所需的背景模式设置为“应用程序播放音频”进入info.plist 2) 在应用程序中:使用选项完成启动: a) 将音频会话类别设置为播放 b) 使音频会话处于活动状态。 c) 使应用程序接收远程控制事件并成为第一响应者 - (BOOL)application:(UIApplication *)application didFinishLaunching

我已经搜索了所有地方,但无法使其始终有效。我想在应用程序处于后台或锁定屏幕且振铃器关闭时,在收到远程推送通知时播放音频

我遵循的步骤是:

1) 将所需的背景模式设置为“应用程序播放音频”进入info.plist

2) 在应用程序中:使用选项完成启动: 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) 在ApplicationIdentinterBackground中: 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];
}

它有时有效,但并不总是有效。你知道如何使这项工作始终如一吗?

我认为你不能在已经处于后台的情况下开始播放背景音频,即使是在
ApplicationIdentinterBackground
中开始也为时已晚。你可以做的一项工作是播放无声音频或暂停音频。您可以使用队列播放器来实现这一点,方法是循环静音音频,并在其结束时寻找开始,然后将您想播放的音频排队。

不要在
应用程序中设置音频会话:使用选项完成启动:
应用程序标识符背景中的remoteEvent listener
尝试仅在
didReceiveRemoteNotification
中设置它们。可能会发生会话被其他应用程序覆盖的情况。@Aanabidden谢谢,我试过了,但无法播放。我还刚刚在DidReceiveMemoteNotification中设置了audioSession,并在didEnterBackground中留下beginReceivingRemoteControlEvents和beginBackgroundTaskWithExpirationHandler,它有时有效,但并不总是有效。我不知该怎么想。还有什么想法吗?