我可以在播放新声音时躲避背景音频.但是我无法再次将背景音频级别恢复到最大值.当我的代表试图“解开”它只是一直被躲避.对此的正常修复是Au@R_502_410@sessionSetProperty,但在iOS 7中已弃用,Apple未在弃用警告或文档中提供任何提示.
- (void) configureAVAu@R_502_410@Session { //get your app's au@R_502_410@Session singleton object AVAu@R_502_410@Session* session = [AVAu@R_502_410@Session sharedInstance]; //error handling BOOL success; NSError* error; success=[session setCategory:AVAu@R_502_410@SessionCategoryPlayback withOptions:AVAu@R_502_410@SessionCategoryOptionMixWithOthers error:&error]; if (!success) { NSLog(@"AVAu@R_502_410@Session 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.au@R_502_410@Player = [[AVAu@R_502_410@Player alloc] initWithContentsOfURL:url error:nil]; self.au@R_502_410@Player.delegate=(id<AVAu@R_502_410@PlayerDelegate>)self; //get your app's au@R_502_410@Session singleton object AVAu@R_502_410@Session* session = [AVAu@R_502_410@Session sharedInstance]; //error handling BOOL success; NSError* error; success=[session setCategory:AVAu@R_502_410@SessionCategoryPlayback withOptions:AVAu@R_502_410@SessionCategoryOptionDuckOthers error:&error]; [self.au@R_502_410@Player prepareToPlay]; [self.au@R_502_410@Player play]; Au@R_502_410@ServicesPlaySystemSound(kSystemSoundID_Vibrate); }
完成音频以恢复背景音频和取消停止音频时,这是我的代表
- (void)au@R_502_410@PlayerDidFinishPlaying:(AVAu@R_502_410@Player *)data successfully:(BOOL)flag{ [self configureAVAu@R_502_410@Session]; NSLog(@"playback ended"); }
那么如何在没有弃用API的情况下再次取消背景音乐?调用[self configureAVAu@R_502_410@Session];显然不起作用….
解决方法
女士们,先生们,我为您提供了一个非弃用的工作示例,说明如何在iOS 7中正确使用闪避.
在您的“视图加载方法”中,调用此方法,其中bool设置为YES.这将混合背景音频并准备用于躲避
- (void) configureAVAu@R_502_410@Session:(bool) active { //get your app's au@R_502_410@Session singleton object AVAu@R_502_410@Session* session = [AVAu@R_502_410@Session sharedInstance]; //error handling BOOL success; NSError* error; success=[session setCategory:AVAu@R_502_410@SessionCategoryPlayback withOptions:AVAu@R_502_410@SessionCategoryOptionMixWithOthers error:&error]; if (!success) { NSLog(@"AVAu@R_502_410@Session error :%@",error); } else { } success = [session setActive:active error:&error]; if (!success) { NSLog(@"Error setting active %@",error); } else { //NSLog(@"success settings active"); } }
使用此方法播放音频文件.观察如何发生躲避..记得在每个播放新的音频警报时,在此示例中设置代理.在视图加载方法中不要设置一次.
-(void)playTimeOnGo { NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] //alertName is the name of your au@R_502_410@ file without extention. extenstions is,doh extenstion like "mp" pathForResource:_dataManager.optionsSettings.setStarts.alertName ofType:_dataManager.optionsSettings.setStarts.alertExtension]]; self.au@R_502_410@Player = [[AVAu@R_502_410@Player alloc] initWithContentsOfURL:url error:nil]; self.au@R_502_410@Player.delegate=(id<AVAu@R_502_410@PlayerDelegate>)self; //get your app's au@R_502_410@Session singleton object AVAu@R_502_410@Session* session = [AVAu@R_502_410@Session sharedInstance]; //error handling BOOL success; NSError* error; success=[session setCategory:AVAu@R_502_410@SessionCategoryPlayback withOptions:AVAu@R_502_410@SessionCategoryOptionDuckOthers error:&error]; [self.au@R_502_410@Player prepareToPlay]; [self.au@R_502_410@Player play]; Au@R_502_410@ServicesPlaySystemSound(kSystemSoundID_Vibrate); }
代表将在播放后调用此方法并调高背景音乐的音量:)请不要混淆我正在使用线程.如果你愿意,你可以调用方法,但是这会使主线程停止大约一秒甚至两秒钟.所以这对你好,不要使用线程,只需调用[self configureAVAu@R_502_410@Session:NO];
- (void)au@R_502_410@PlayerDidFinishPlaying:(AVAu@R_502_410@Player *)data successfully:(BOOL)flag { _playBackFinishedDelegateThead = [[NSThread alloc] initWithTarget:self selector:@selector(configureAVAu@R_502_410@Session:) object:NO]; [_playBackFinishedDelegateThead start]; }
此示例已经过100%测试并在我的应用中运行.