ios 7非弃用的解决方案,用于恢复来自duck的背景音乐

aZt*_*ceR 7 background deprecated audiosession ios7

我可以在播放新声音时播放背景音频.但是我无法再次将背景音频级别恢复到最大值.当我的代表试图"解开"它只是一直被躲避.对此的正常修复是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");
    }


}
Run Code Online (Sandbox Code Playgroud)

这是我播放音频的时候

-(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);
}
Run Code Online (Sandbox Code Playgroud)

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

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)data successfully:(BOOL)flag{

    [self configureAVAudioSession];

    NSLog(@"playback ended");

}
Run Code Online (Sandbox Code Playgroud)

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

aZt*_*ceR 4

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

在您的任何“视图加载方法”中调用此方法,其中布尔值设置为 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");
        }

 }
Run Code Online (Sandbox Code Playgroud)

使用此方法播放音频文件。观察闪避是如何发生的......记住每次播放新的音频警报时都像我在本示例中那样设置委托。不要在视图加载方法中设置它一次。

-(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);
}
Run Code Online (Sandbox Code Playgroud)

委托将在播放后调用此方法并调高背景音乐的音量:) 请不要对我为此使用线程感到困惑。如果您愿意,您可以调用该方法,但这会使主线程停止大约一秒甚至两秒。所以没关系,不要使用线程,只需调用[self configureAVAudioSession:NO];

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)data successfully:(BOOL)flag
{
    _playBackFinishedDelegateThead = [[NSThread alloc] initWithTarget:self selector:@selector(configureAVAudioSession:) object:NO];
    [_playBackFinishedDelegateThead start];

}
Run Code Online (Sandbox Code Playgroud)

此示例经过 100% 测试并可在我的应用程序中运行。