锁定后,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]; }
- 为什么你有时候会把一个NSArray转换成NSMutableArray,有时你不能呢?
- Swift 2在调用中额外的参数“错误”
- 无法使用AFNetworking 3为JSON和XML响应创build单例
- SWIFT:错误“线程1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)”是什么意思?
- 以编程方式创建ViewController时,如何将其视图设置为SKVIew类型?
- 借助新的iMessage集成,可以更轻松地进行团体订购
- 使用AVFoundation的video的第一帧
- 我的应用程序在我的iPhone上运行时崩溃,但不在模拟器中。 我可以用什么debugging技术来解决这个问题?
- 我的admob横幅显示在顶部