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

前端之家收集整理的这篇文章主要介绍了ios 7非弃用解决方案从鸭子恢复背景音乐前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我可以在播放新声音时躲避背景音频.但是我无法再次将背景音频级别恢复到最大值.当我的代表试图“解开”它只是一直被躲避.对此的正常修复是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];显然不起作用….

解决方法

女士们,先生们,我为您提供了一个非弃用的工作示例,说明如何在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%测试并在我的应用中运行.

原文链接:https://www.f2er.com/iOS/331403.html

猜你在找的iOS相关文章