我在加载视图时调用此方法.
- (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];显然不起作用….
解决方法 女士们,先生们,我为您提供了一个非弃用的工作示例,说明如何在iOS 7中正确使用闪避.在您的“视图加载方法”中,调用此方法,其中bool设置为YES.这将混合背景音频并准备用于躲避
- (voID) configureAVAudioSession:(bool) active { //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: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 audio file without extention. extenstions is,doh extenstion like "mp" pathForResource:_dataManager.optionsSettings.setStarts.alertname ofType:_dataManager.optionsSettings.setStarts.alertExtension]]; 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);}
代表将在播放后调用此方法并调高背景音乐的音量:)请不要混淆我正在使用线程.如果你愿意,你可以调用方法,但是这会使主线程停止大约一秒甚至两秒钟.所以这对你好,不要使用线程,只需调用[self configureAVAudioSession:NO];
- (voID)audioPlayerDIDFinishPlaying:(AVAudioPlayer *)data successfully:(BOol)flag{ _playBackFinishedDelegateThead = [[NSThread alloc] initWithTarget:self selector:@selector(configureAVAudioSession:) object:NO]; [_playBackFinishedDelegateThead start];}
此示例已经过100%测试并在我的应用中运行.
总结以上是内存溢出为你收集整理的ios 7非弃用解决方案从鸭子恢复背景音乐全部内容,希望文章能够帮你解决ios 7非弃用解决方案从鸭子恢复背景音乐所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)