Cocos2D 2.1: “Delegate” deprecated in iOS 6. How do I set the delegate for this AVAudioSession?

后端 未结 4 1487
一整个雨季
一整个雨季 2021-02-05 19:17

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...

AVAudioSe         


        
4条回答
  •  温柔的废话
    2021-02-05 19:41

    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.

提交回复
热议问题