2012-10-25 2 views
11

iOS 6 및 iPad를 대상으로 Xcode 4.5에서 (물리 엔진이없는) Cocos2D 2.1 템플릿을 시작했습니다. CDAudioManager.m 파일, 다음 코드 ...Cocos2D 2.1 : iOS 6에서 'Delegate'가 사용되지 않음.이 AVAudioSession에 대한 대리자를 어떻게 설정합니까?

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

에서 ... 다음과 같은 경고를 생성 ...

"delegate deprecated: first deprecated in iOS 6" 

그래서 나는 애플 개발자 문서로 이동하고, 아래에 말한다 "delegate", "iOS 6.0에서는 더 이상 사용되지 않으며 대신이 클래스의 Notifications 섹션에 설명 된 알림을 사용하십시오." 내 경험 부족을 용서 - -

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

문제는 우리가하려는 모든처럼 나에게 보이는,있는 CDAudioManager 인스턴스 자체에 AVAudioSession의 대리자를 설정합니다. 통지는 어떻게 이것을 달성합니까? 또는 위 코드의 목표에 대해 잘못 생각한 것입니까?

답변

17

당신이로 실행하는 오류는 경고가 여기에 사람들이 줄을 변경 침묵 코드

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

의이 블록에 있습니다

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

희망이 도움이됩니다.

+10

'이유는 무엇입니까?' – Jonny

+0

Apple은 iOS 6에서 위임자와 AVAudioSessionDelegate 프로토콜을 설정하지 않으므로 대신 NSNotification 센터를 통해 알림을 청취해야합니다. – geekinit

+9

이 답변은 불완전한 것으로 보입니다. –

9

나는 이것에 대해 Cocos2D-iPhone.org 포럼에서 시끄러운 것을 발견했습니다. 나는 그것을 완전히 이해하지 못하고 있지만, 나는 그것에 대해 연구 중이다. 적어도 일시적으로 문제를 해결하는 것처럼 보였다. 링크 여기

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

것 : http://www.cocos2d-iphone.org/forum/topic/49956

-(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; 

그가했던 것은 CDAudioManger.m 파일에서이 방법을 쓰기이었다

이 코드의 기능을 더 잘 이해할 수있게되면 t를 편집해야합니다. 그의 게시물.

+0

내가 CDAudioManager이없는 따른다.m – CroiOS

+0

당신이 보여준 링크는 새로운 양식 NSNotification 센터 사용과 관련된 문제에 대해서도 이야기하고, iOS 5.0이있는 해당 라인에 잘못된 액세스 오류를 던졌습니다. sooo 구체적인 해결책은 무엇입니까? 그들은 iOS 버전 체크를하고 그에 따라 코드를 던질 것을 제안합니다. –

+1

+1 "poast". – Jonny

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 버전에 따라 다른 코드 런타임을 던집니다.

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

그리고 내 엄지 손가락을 교차 : 난 그냥 갈거야, 그래서

지금, 내 응용 프로그램은 어쨌든 iOS 6.0 이상이다. 대신으로 처리하기위한 대의원 사용 알림을 사용

10

[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]; 
}