A fix for AudioSessionInitialize Deprecated?

前端 未结 5 1073
半阙折子戏
半阙折子戏 2021-02-05 07:31

Apple did not post any alternative code for this on the Apple Developer site.

相关标签:
5条回答
  • 2021-02-05 08:06

    Swift version:

    do {
        try AVAudioSession.sharedInstance().setActive(true)
    } catch let error {
        print("\(error.localizedDescription)")
    }
    
    do {
        try AVAudioSession.sharedInstance().setCategory(.playback, mode: .spokenAudio)
    } catch let error {
        print("\(error.localizedDescription)")
    }
    
    0 讨论(0)
  • 2021-02-05 08:16

    1. for this code

    AudioSessionInitialize( NULL, NULL, interruptionCallback, self );
    

    replace with

    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    

    2. fro this code

    UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
    AudioSessionSetProperty(
            kAudioSessionProperty_AudioCategory,
            sizeof(sessionCategory),
            &sessionCategory
            );
    

    replace with

    UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
    [[AVAudioSession sharedInstance]
         setCategory:sessionCategory error:nil];
    
    0 讨论(0)
  • 2021-02-05 08:20

    You should use AVAudioSession.

    To replace the functionality provided by deprecated AudioSessionInitialize (e.g. if you need to specify AudioSessionInterruptionListener callback) you can subscribe for AVAudioSessionInterruptionNotification notification:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionDidChangeInterruptionType:)
            name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];
    

    And implement your audioSessionDidChangeInterruptionType: handler like:

    - (void)audioSessionDidChangeInterruptionType:(NSNotification *)notification
    {
        AVAudioSessionInterruptionType interruptionType = [[[notification userInfo]
            objectForKey:AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
        if (AVAudioSessionInterruptionTypeBegan == interruptionType)
        {
        }
        else if (AVAudioSessionInterruptionTypeEnded == interruptionType)
        {
        }
    }
    
    0 讨论(0)
  • 2021-02-05 08:20

    The equivalent code to

    // C way
    UInt32 category = kAudioSessionCategory_MediaPlayback ;
    OSStatus result = AudioSessionSetProperty(
      kAudioSessionProperty_AudioCategory, sizeof(category), &category ) ;
    
    if( result ) // handle the error
    

    Is

    // Objective-C way
    NSError *nsError;
    [[AVAudioSession sharedInstance]
      setCategory:AVAudioSessionCategoryPlayback error:&nsError];
    
    if( nsError != nil )  // handle the error
    
    0 讨论(0)
  • 2021-02-05 08:23

    In swift we can add the following

    let audioSession = AVAudioSession.sharedInstance()
            do {
                try audioSession.setActive(true)
                try audioSession.setCategory(AVAudioSessionCategoryPlayback)
            } catch {
                print("Setting category to AVAudioSessionCategoryPlayback failed.")
            }
    

    from: https://developer.apple.com/documentation/avfoundation/avaudiosession

    0 讨论(0)
提交回复
热议问题