13

Started a Cocos2D 2.1 template (with no physics engine) in Xcode 4.5, targeted for iOS 6 and iPad. In the CDAudioManager.m file, the following code...

AVAudioSession* session = [AVAudioSession sharedInstance];
session.delegate = self;  // Which is what is automatically generated by the template.

...generates the following warning...

"delegate deprecated:  first deprecated in iOS 6"

So I go to the apple developer documentation, and it says under "delegate," "Deprecated in iOS 6.0. Use the notifications described in the Notifications section of this class instead."

http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioSession_ClassReference/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instp/AVAudioSession/delegate

Problem is, it looks to me like all we're trying to do--forgive my inexperience--is set the delegate for the AVAudioSession to the CDAudioManager instance itself. How do the notifications accomplish this? Or am I just wrong about the goal of the above code?

4 Answers 4

16

The error you are running into is in this block of code

AVAudioSession* session = [AVAudioSession sharedInstance];
session.delegate = self;// <-------- DEPRECATED IN IOS 6.0

To silence the warning change those 2 lines to this:

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

Hope this helps.

2
  • Apple deprecated setting the delegate and the AVAudioSessionDelegate protocol in iOS 6 and now you have to listen to the notifications instead through NSNotification center.
    – geekinit
    Dec 28, 2013 at 19:34
  • 10
    This answer looks incomplete. Jan 14, 2014 at 4:51
11

Instead of using delegate use notification for handling as follows

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

 - (void) interruption:(NSNotification*)notification
   {
    NSDictionary *interuptionDict = notification.userInfo;

    NSUInteger interuptionType = (NSUInteger)[interuptionDict valueForKey:AVAudioSessionInterruptionTypeKey];

   if (interuptionType == AVAudioSessionInterruptionTypeBegan)
        [self beginInterruption];

    else if (interuptionType == AVAudioSessionInterruptionTypeEnded)
        [self endInterruption];
}
9

I found a poast about this on the Cocos2D-iPhone.org forums. While I don't fully understand it--but I'm working on it--it did seem to take care of the problem, at least temporarily. What he did was write this method in the CDAudioManger.m file:

-(void) releaseBufferForFile:(NSString *) filePath {
    int bufferId = [self bufferForFile:filePath create:NO];
    if (bufferId != kCDNoBuffer) {
        [soundEngine unloadBuffer:bufferId];
        [loadedBuffers removeObjectForKey:filePath];
        NSNumber *freedBufferId = [[NSNumber alloc] initWithInt:bufferId];
        [freedBufferId autorelease];
        [freedBuffers addObject:freedBufferId];
    }
}
@end

- (void) interruption:(NSNotification*)notification
{
    NSDictionary *interuptionDict = notification.userInfo;
            NSNumber* interuptionTypeValue = [dict valueForKey:AVAudioSessionInterruptionTypeKey];
    NSUInteger interuptionType = [interuptionTypeValue intValue];

    if (interuptionType == AVAudioSessionInterruptionTypeBegan)
        [self beginInterruption];
#if __CC_PLATFORM_IOS >= 40000
    else if (interuptionType == AVAudioSessionInterruptionTypeEnded)
        [self endInterruptionWithFlags:(NSUInteger)[interuptionDict valueForKey:AVAudioSessionInterruptionOptionKey]];
#else
    else if (interuptionType == AVAudioSessionInterruptionTypeEnded)
        [self endInterruption];
#endif
}

Then he replaced:

AVAudioSession *session = [AVAudioSession sharedInstance];
session.delegate = self;

with this:

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

Here's the link: http://www.cocos2d-iphone.org/forum/topic/49956

If and when I develop a better understand of what this code is doing, I'll be sure to edit this post.

4
  • I don't have CDAudioManager.m
    – iWizard
    Dec 20, 2012 at 13:16
  • The link you showed also talks about issues with using that new form the NSNotification center, throws a Bad Access error in that line with iOS 5.0... sooo what would be a concrete solution? they suggest having a iOS version check and throw code accordingly.
    – S.H.
    Apr 29, 2013 at 2:44
  • 1
    This line is just wrong: NSUInteger interuptionType = (NSUInteger)[interuptionDict valueForKey:AVAudioSessionInterruptionTypeKey]; It should be NSNumber* interuptionType = [dict valueForKey:AVAudioSessionInterruptionTypeKey]; switch ( [interuptionType integerValue] ) { Nov 26, 2013 at 9:50
  • #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000 else if (interuptionType == AVAudioSessionInterruptionTypeEnded) { NSNumber *optionsNumber = interuptionDict[AVAudioSessionInterruptionOptionKey]; AVAudioSessionInterruptionOptions options = [optionsNumber integerValue]; [self endInterruptionWithFlags:options]; } #else May 28, 2015 at 22:55
0

I haven't tested this but according to this post: http://www.cocos2d-iphone.org/forums/topic/cdaudiomanager-line-402-delegate-is-deprecated/#post-390211

float osVersion = [[UIDevice currentDevice].systemVersion floatValue];
if (osVersion >=6.0)
{
[AVAudioSession sharedInstance];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(interruption:) name:AVAudioSessionInterruptionNotification object:nil];
}
else
{
AVAudioSession* session = [AVAudioSession sharedInstance];
session.delegate = self;
}

I.e. throw different code runtime depending on iOS version.

Now, my app is iOS 6.0+ only anyway so I'll just go with:

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

And cross my thumbs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.