我可以在播放新声音时回避我的背景音频。但是,我无法再次将背景音频级别恢复到最大值。当我的代表试图“取消”时,它只是一直被躲避。对此的正常修复是 AudiosessionSetProperty,但在 iOS 7 中已弃用,Apple 并未在弃用警告或文档中提供任何提示。
我在加载视图时调用此方法。
- (void) configureAVAudioSession
{
//get your app's audioSession singleton object
AVAudioSession* session = [AVAudioSession sharedInstance];
//error handling
BOOL success;
NSError* error;
success=[session setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error];
if (!success)
{
NSLog(@"AVAudioSession error :%@",error);
}
else
{
}
success = [session setActive:YES error:&error];
if (!success) {
NSLog(@"Error setting active %@",error);
}
else
{
NSLog(@"succes settings active");
}
}
这是我播放音频的时候
-(void)playTimeOnGo
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"just-like-magic"
ofType:@"mp3"]];
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
self.audioPlayer.delegate=(id<AVAudioPlayerDelegate>)self;
//get your app's audioSession singleton object
AVAudioSession* session = [AVAudioSession sharedInstance];
//error handling
BOOL success;
NSError* error;
success=[session setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDuckOthers error:&error];
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
这是我在完成音频以恢复背景音频和取消停靠音频时的代表
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)data successfully:(BOOL)flag{
[self configureAVAudioSession];
NSLog(@"playback ended");
}
那么如何在没有弃用 API 的情况下再次取消背景音乐呢?调用 [self configureAVAudioSession];
显然不起作用....