[英]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.