Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A fix for AudioSessionInitialize Deprecated?

Tags:

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

like image 427
Marsman Avatar asked Oct 31 '13 15:10

Marsman


3 Answers

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)
    {
    }
}
like image 196
Pleskach Yuriy Avatar answered Oct 20 '22 08:10

Pleskach Yuriy


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];
like image 43
Pooja Patel Avatar answered Oct 20 '22 08:10

Pooja Patel


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
like image 30
bobobobo Avatar answered Oct 20 '22 08:10

bobobobo