ios 7非弃用解决方案从鸭子恢复背景音乐

前端之家收集整理的这篇文章主要介绍了ios 7非弃用解决方案从鸭子恢复背景音乐前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我可以在播放新声音时躲避背景音频.但是我无法再次将背景音频级别恢复到最大值.当我的代表试图“解开”它只是一直被躲避.对此的正常修复是AudiosessionSetProperty,但在iOS 7中已弃用,Apple未在弃用警告或文档中提供任何提示.

我在加载视图时调用方法.

  1. - (void) configureAVAudioSession
  2. {
  3. //get your app's audioSession singleton object
  4. AVAudioSession* session = [AVAudioSession sharedInstance];
  5.  
  6. //error handling
  7. BOOL success;
  8. NSError* error;
  9.  
  10.  
  11. success=[session setCategory:AVAudioSessionCategoryPlayback
  12. withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error];
  13.  
  14.  
  15.  
  16. if (!success)
  17. {
  18. NSLog(@"AVAudioSession error :%@",error);
  19.  
  20. }
  21. else
  22. {
  23.  
  24. }
  25. success = [session setActive:YES error:&error];
  26.  
  27. if (!success) {
  28. NSLog(@"Error setting active %@",error);
  29. }
  30. else
  31. {
  32. NSLog(@"succes settings active");
  33. }
  34.  
  35.  
  36. }

这是我播放音频的时候

  1. -(void)playTimeOnGo
  2. {
  3.  
  4.  
  5. NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
  6. pathForResource:@"just-like-magic"
  7. ofType:@"mp3"]];
  8. self.audioPlayer = [[AVAudioPlayer alloc]
  9. initWithContentsOfURL:url
  10. error:nil];
  11. self.audioPlayer.delegate=(id<AVAudioPlayerDelegate>)self;
  12.  
  13. //get your app's audioSession singleton object
  14. AVAudioSession* session = [AVAudioSession sharedInstance];
  15.  
  16. //error handling
  17. BOOL success;
  18. NSError* error;
  19.  
  20.  
  21. success=[session setCategory:AVAudioSessionCategoryPlayback
  22. withOptions:AVAudioSessionCategoryOptionDuckOthers error:&error];
  23.  
  24. [self.audioPlayer prepareToPlay];
  25. [self.audioPlayer play];
  26.  
  27.  
  28. AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
  29. }

完成音频以恢复背景音频和取消停止音频时,这是我的代表

  1. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)data successfully:(BOOL)flag{
  2.  
  3. [self configureAVAudioSession];
  4.  
  5. NSLog(@"playback ended");
  6.  
  7. }

那么如何在没有弃用API的情况下再次取消背景音乐?调用[self configureAVAudioSession];显然不起作用….

解决方法

女士们,先生们,我为您提供了一个非弃用的工作示例,说明如何在iOS 7中正确使用闪避.

在您的“视图加载方法”中,调用方法,其中bool设置为YES.这将混合背景音频并准备用于躲避

  1. - (void) configureAVAudioSession:(bool) active
  2. {
  3. //get your app's audioSession singleton object
  4. AVAudioSession* session = [AVAudioSession sharedInstance];
  5.  
  6. //error handling
  7. BOOL success;
  8. NSError* error;
  9.  
  10.  
  11. success=[session setCategory:AVAudioSessionCategoryPlayback
  12. withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error];
  13.  
  14.  
  15.  
  16. if (!success)
  17. {
  18. NSLog(@"AVAudioSession error :%@",error);
  19.  
  20. }
  21. else
  22. {
  23.  
  24. }
  25. success = [session setActive:active error:&error];
  26.  
  27. if (!success) {
  28. NSLog(@"Error setting active %@",error);
  29. }
  30. else
  31. {
  32. //NSLog(@"success settings active");
  33. }
  34.  
  35. }

使用此方法播放音频文件.观察如何发生躲避..记得在每个播放新的音频警报时,在此示例中设置代理.在视图加载方法中不要设置一次.

  1. -(void)playTimeOnGo
  2. {
  3.  
  4.  
  5. NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
  6. //alertName is the name of your audio file without extention. extenstions is,doh extenstion like "mp"
  7. pathForResource:_dataManager.optionsSettings.setStarts.alertName
  8. ofType:_dataManager.optionsSettings.setStarts.alertExtension]];
  9. self.audioPlayer = [[AVAudioPlayer alloc]
  10. initWithContentsOfURL:url
  11. error:nil];
  12. self.audioPlayer.delegate=(id<AVAudioPlayerDelegate>)self;
  13.  
  14. //get your app's audioSession singleton object
  15. AVAudioSession* session = [AVAudioSession sharedInstance];
  16.  
  17. //error handling
  18. BOOL success;
  19. NSError* error;
  20.  
  21.  
  22. success=[session setCategory:AVAudioSessionCategoryPlayback
  23. withOptions:AVAudioSessionCategoryOptionDuckOthers error:&error];
  24.  
  25. [self.audioPlayer prepareToPlay];
  26. [self.audioPlayer play];
  27.  
  28.  
  29. AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
  30. }

代表将在播放后调用方法并调高背景音乐的音量:)请不要混淆我正在使用线程.如果你愿意,你可以调用方法,但是这会使主线程停止大约一秒甚至两秒钟.所以这对你好,不要使用线程,只需调用[self configureAVAudioSession:NO];

  1. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)data successfully:(BOOL)flag
  2. {
  3. _playBackFinishedDelegateThead = [[NSThread alloc] initWithTarget:self selector:@selector(configureAVAudioSession:) object:NO];
  4. [_playBackFinishedDelegateThead start];
  5.  
  6. }

此示例已经过100%测试并在我的应用中运行.

猜你在找的iOS相关文章