简体   繁体   English

iOS 7:音频无法在后台播放

[英]iOS 7: Audio not playing in background

I'm writing an iPhone app that plays sound at regular intervals via a timer. 我正在编写一个iPhone应用程序,该应用程序通过计时器定期播放声音。 The first version of this app played audio just fine when it was minimized, or when the screen went blank, however, after adding some UI, it looks like this has stopped. 此应用程序的第一个版本在最小化或屏幕变黑时可以正常播放音频,但是,在添加了一些UI后,看起来好像已经停止了。 I'm not sure why, or even how to diagnose this (removing the UI code doesn't solve the issue). 我不确定原因,甚至如何诊断(删除UI代码无法解决问题)。

I've set the background mode in the info.plist file: ("App plays audio or streams audio/video via Airplay") and I've also got the following in my app delegate: 我已经在info.plist文件中设置了背景模式:(“应用程序通过Airplay播放音频或流音频/视频”),并且我的应用程序委托中还包含以下内容:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // Override point for customization after application launch.
  NSError *setCategoryErr = nil;
  NSError *activationErr  = nil;

  [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback 
                                     error:&setCategoryErr];

  [[AVAudioSession sharedInstance] setActive:YES 
                                   error:&activationErr];  
  return YES;  
}

And in my ViewDidLayoutSubViews method, I initialize AVPLayer thusly: 然后在我的ViewDidLayoutSubViews方法中,初始化AVPLayer:

  self.fAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
  self.fAudioPlayer.numberOfLoops = 0;
  [self.fAudioPlayer setVolume:volume];
  [self.fVolumeLevelSlider setValue:volume];  

Where fAudioPlayer is of type AVAudioPlayer. 其中fAudioPlayer是AVAudioPlayer类型。 When my timer is hit, I play the audio like this: 按下计时器后,我将像这样播放音频:

  self.fBGTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
  [self.fAudioPlayer play];

I'm still going through my changes to see what, if anything, I had put in that could cause the sound to stop playing in the background from one app version to the next. 我仍在进行更改,以查看输入的内容(如果有的话)可能会导致从一个应用程序版本到另一个应用程序版本在后台停止播放。 Does anyone see anything here that could be amiss? 有人在这里看到什么不对劲吗?

Moving these lines: 移动这些行:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];

Right before I start playing in my timer, resolved the issue. 在我开始使用计时器之前,已经解决了该问题。

In order to keep the player to keep playing even when the app is in background, you have to update the application's info.plist file by adding the following key 为了使播放器即使在后台运行时也能继续播放,您必须通过添加以下键来更新应用程序的info.plist文件

"Required background modes" and add the value to "App plays audio or streams audio/video using AirPlay" “必需的背景模式”,并将其值添加到“应用播放音频或使用AirPlay播放音频/视频流”

Setting this property allows the app to even play/stream Audio in background, while app is minimized or another app is open (including screen saver). 设置此属性可以使该应用程序甚至在后台播放/流音频,同时最小化该应用程序或打开另一个应用程序(包括屏幕保护程序)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM