Cocos2D 2.1:iOS 6中不推荐使用"Delegate".如何设置此AVAudioSession的委托?

bap*_*ire 13 delegates cocos2d-iphone avaudiosession

在Xcode 4.5中启动了一个Cocos2D 2.1模板(没有物理引擎),针对iOS 6和iPad.在CDAudioManager.m文件中,以下代码...

AVAudioSession* session = [AVAudioSession sharedInstance];
session.delegate = self;  // Which is what is automatically generated by the template.
Run Code Online (Sandbox Code Playgroud)

...生成以下警告......

"delegate deprecated:  first deprecated in iOS 6"
Run Code Online (Sandbox Code Playgroud)

所以我转到苹果开发者文档,它在"委托","在iOS 6.0中不推荐使用."请使用此类通知部分中描述的通知.

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

问题是,它看起来就像我们想要做的一样 - 原谅我的经验不足 - 将AVAudioSession的委托设置为CDAudioManager实例本身.通知如何实现这一目标?或者我对上述代码的目标错了吗?

Jas*_* P. 16

您遇到的错误是在这段代码中

AVAudioSession* session = [AVAudioSession sharedInstance];
session.delegate = self;// <-------- DEPRECATED IN IOS 6.0
Run Code Online (Sandbox Code Playgroud)

要使警告静音,请更改以下两行:

[[AVAudioSession sharedInstance] setActive:YES error:nil];
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

  • 这个答案看起来不完整 (10认同)

小智 11

而不是使用委托使用通知进行如下处理

[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];
}
Run Code Online (Sandbox Code Playgroud)


bap*_*ire 9

我在Cocos2D-iPhone.org论坛上发现了一个关于这个问题的大师.虽然我不完全理解它 - 但我正在研究它 - 它似乎确实解决了这个问题,至少是暂时的.他所做的是在CDAudioManger.m文件中编写此方法:

-(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
}
Run Code Online (Sandbox Code Playgroud)

然后他换了:

AVAudioSession *session = [AVAudioSession sharedInstance];
session.delegate = self;
Run Code Online (Sandbox Code Playgroud)

有了这个:

[AVAudioSession sharedInstance];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(interruption:) name:AVAudioSessionInterruptionNotification object:nil];
Run Code Online (Sandbox Code Playgroud)

这是链接:http: //www.cocos2d-iphone.org/forum/topic/49956

如果我更好地理解这段代码的作用,我一定会编辑这篇文章.