如何保持avaudioplayer播放设备(iPhone)锁定状态?

时间:2014-07-08 06:54:38

标签: ios iphone avaudioplayer

我在我的应用程序中使用AVAudioPlayer。我的问题是,当设备被锁定时它没有播放。

2 个答案:

答案 0 :(得分:2)

按照下图中的步骤操作。它应该工作。

enter image description here

答案 1 :(得分: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];
}

Reference Link

希望此代码对您有用。