13

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

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

"delegate deprecated:  first deprecated in iOS 6"

所以我去苹果开发者文档,它在“委托”下说,“在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 实例本身。通知是如何做到这一点的?或者我对上述代码的目标是错误的?

4

4 回答 4

16

您遇到的错误在此代码块中

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

要使警告静音,请将这两行更改为:

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

希望这可以帮助。

于 2012-11-09T21:30:17.337 回答
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];
}
于 2013-10-04T09:51:39.523 回答
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
}

然后他换了:

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

有了这个:

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

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

如果我对这段代码的作用有了更好的理解,我一定会编辑这篇文章。

于 2012-10-26T18:21:57.867 回答
0

我没有对此进行测试,但根据这篇文章: 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;
}

即根据 iOS 版本抛出不同的代码运行时。

现在,我的应用程序只是 iOS 6.0+,所以我会选择:

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

并交叉我的拇指。

于 2013-08-01T01:39:56.510 回答