[英]AVAudioPlayer doesn't play audio in iPhone 5 when locked
我正在尝试使用AVAudioPlayer在播放iphone播放器时播放声音。 同样在设备锁定时。 事实是,在iPhone 4s ios 7中-可以正常工作。 但是在具有6和7 ios的iPhone 5上,什么也没有发生。
在-Info.plist
的Required background modes
我编写了
App plays audio or streams audio/video using AirPlay
在Player.h
实现:
@property (nonatomic, strong) AVAudioPlayer *notificationPlayer;
包含带有#import <AVFoundation/AVFoundation.h>
<AVAudioPlayerDelegate, AVAudioSessionDelegate>
同样在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"]];
}
问候。
在后台播放声音时要注意的另一个非常重要的事情是您是否拥有该文件的权限。 如果您正在播放捆绑文件,则具有权限,并且大概是从互联网流式传输的某些文件,但是如果您已将媒体下载到documents目录中,则必须适当设置权限,尤其是在您的应用程序本机锁定文件的情况下。
这往往与许多人认为的症状相匹配,在这种症状下,文件将继续播放几秒钟,即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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.