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

 mobiledu2502927445 发布于 2023-02-12 12:13

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

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有