锁定后,AVAudioPlayer不会在iPhone 5中播放音频

使用AVAudioPlayer我试图在iphone播放器播放时播放声音。 当设备被锁定时。 问题是,在iPhone 4s ios 7中 – 工作正常。 但在拥有6和7 ios的iPhone 5上没有任何反应。

在我写的Required background modes-Info.plist

App plays audio or streams audio/video using AirPlay

Player.h实现:

 @property (nonatomic, strong) AVAudioPlayer *notificationPlayer; 

包含#import

也在Player.m

 //this is called on viewDidLoad -(void) prepareSound{ [[AVAudioSession sharedInstance] setDelegate:self]; } //overriden function - (void)playAudio:(NSString *)path{ [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient withOptions: AVAudioSessionCategoryOptionDuckOthers error: nil]; NSError *error; NSURL *audioURL = [NSURL fileURLWithPath:path]; self.notificationPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:audioURL error:&error]; self.notificationPlayer.delegate = self; [self.notificationPlayer prepareToPlay]; [self.notificationPlayer setVolume:1.0]; [self.notificationPlayer play]; if (error) { NSLog(@"error %@",[error localizedDescription]); } } - (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ [player stop]; [[AVAudioSession sharedInstance] setActive:NO withOptions:0 error:nil]; [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient withOptions: 0 error: nil]; [[AVAudioSession sharedInstance] setActive:YES withOptions: 0 error:nil]; } //Call from here -(void) customMethod{ [self playAudio:[[NSBundle mainBundle] pathForResource:@"3" ofType:@"wav"]]; } 

问候。

在后台播放声音时要注意的另一个非常重要的事情是你是否拥有该文件的权限。 如果您正在播放捆绑文件,则您具有权限,并且可能是从Internet上流式传输的文件,但如果您已将媒体下载到文档目录中,则必须正确设置权限,尤其是在应用程序本机锁定文件时。

这往往与许多人认为文件将继续播放几秒钟,5-15然后停止的症状相匹配。 如果您正在侦听已完成的方法,则会看到错误标志。 原因:AVAudioPlayer在您第一次播放时不会将整个音频文件加载到内存中。
例:

  NSURL url = [NSURL URLWithString:@""]; NSError *error = nil; [[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey : NSFileProtectionCompleteUntilFirstUserAuthentication} ofItemAtPath:[url path] error:&error]; 

您的选择如下:

  NSFileProtectionComplete; NSFileProtectionCompleteUnlessOpen; NSFileProtectionCompleteUntilFirstUserAuthentication; NSFileProtectionNone; 

我应该使用以下代码

 - (void)playAudio:(NSString *)path { NSError *error; NSURL *audioURL = [NSURL fileURLWithPath:path]; self.notificationPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:audioURL error:&error]; self.notificationPlayer.delegate = self; AVAudioSession* audioSession = [AVAudioSession sharedInstance]; NSError *errorSession = nil; [audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDuckOthers error:nil]; [audioSession setActive:NO error:&errorSession]; [self.notificationPlayer prepareToPlay]; [self.notificationPlayer setVolume:1.0]; [self.notificationPlayer play]; if (error) { NSLog(@"error %@",[error localizedDescription]); } NSLog(@"Should play music"); } - (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ [player stop]; [[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]; }