20

Apple 没有在 Apple Developer 网站上为此发布任何替代代码。

4

5 回答 5

11

您应该使用 AVAudioSession。

要替换已弃用的 AudioSessionInitialize 提供的功能(例如,如果您需要指定 AudioSessionInterruptionListener 回调),您可以订阅 AVAudioSessionInterruptionNotification 通知:

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

并实现您的 audioSessionDidChangeInterruptionType: 处理程序,如:

- (void)audioSessionDidChangeInterruptionType:(NSNotification *)notification
{
    AVAudioSessionInterruptionType interruptionType = [[[notification userInfo]
        objectForKey:AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
    if (AVAudioSessionInterruptionTypeBegan == interruptionType)
    {
    }
    else if (AVAudioSessionInterruptionTypeEnded == interruptionType)
    {
    }
}
于 2013-11-04T16:57:50.823 回答
10

1.对于这个代码

AudioSessionInitialize( NULL, NULL, interruptionCallback, self );

用。。。来代替

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

2.从此代码

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

用。。。来代替

UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
[[AVAudioSession sharedInstance]
     setCategory:sessionCategory error:nil];
于 2015-09-02T09:17:47.717 回答
6

等效的代码

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

if( result ) // handle the error

// Objective-C way
NSError *nsError;
[[AVAudioSession sharedInstance]
  setCategory:AVAudioSessionCategoryPlayback error:&nsError];

if( nsError != nil )  // handle the error
于 2013-12-25T18:46:22.487 回答
1

在swift中,我们可以添加以下内容

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

来自:https ://developer.apple.com/documentation/avfoundation/avaudiosession

于 2018-09-21T09:59:12.067 回答
0

斯威夫特版本:

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)")
}
于 2018-11-07T20:16:22.607 回答