2013-10-24 86 views
2

使用AVAudioPlayer我试图在iphone播放器播放时播放声音。此外当设备被锁定。 事情是,在iPhone 4s ios 7 - 工作正常。 但是,iPhone 6与6和7 ios没有任何反应。AVAudioPlayer在锁定时不会在iPhone 5中播放音频

-Info.plistRequired background modesPlayer.h

App plays audio or streams audio/video using AirPlay

实现:

@property (nonatomic, strong) AVAudioPlayer *notificationPlayer; 

<AVAudioPlayerDelegate, AVAudioSessionDelegate>#import <AVFoundation/AVFoundation.h>包括

另外在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"]]; 
} 

问候。

+0

您确认您已加入该行的'info.plist'为合适的项目目标,而不是'test'目标?我犯了这个错误,花了大约2.5天的时间才弄明白!我认为一切都是正确的,它不工作! – Neeku

+0

@Neeku,谢谢你的回复!是的,我将该行添加到项目目标,而不是测试目标。 –

回答

1

我应该使用下面的代码

- (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]; 
} 
2

另一个真正重要的事情需要注意,而在后台播放声音是你是否没有权限的文件。如果你正在播放一个包文件,你有权限,并且大概有一些文件是从互联网传输过来的,但是如果你已经将媒体下载到文档目录中,那么你必须适当地设置权限,特别是如果你的应用程序本身锁定了文件。

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

NSURL url = [NSURL URLWithString:@""]; 

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

您的选项如下:

NSFileProtectionComplete; 
    NSFileProtectionCompleteUnlessOpen; 
    NSFileProtectionCompleteUntilFirstUserAuthentication; 
    NSFileProtectionNone; 
+0

感谢您提供这些详细信息 –

相关问题