2012-10-25 15 views
11

Xcode 4.5'te, iOS 6 ve iPad için hedeflenen bir Cocos2D 2.1 şablonuna (fizik motoru olmadan) başladı. CDAudioManager.m dosyası, aşağıdaki kodla ...Cocos2D 2.1: iOS'ta kullanımdan kaldırılmış "Temsilci" 6. Bu AVAudioSession için temsilci nasıl ayarlayabilirim?

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

yılında ... Aşağıdaki uyarı oluşturur ...

"delegate deprecated: first deprecated in iOS 6" 

yüzden elma geliştirici dokümanlarına gidin ve altında diyor "temsilci", "iOS 6.0'da kullanımdan kaldırıldı. Bunun yerine, bu sınıfın Bildirimler bölümünde açıklanan bildirimleri kullanın." Benim deneyimsizliklerini affet - -

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

Sorun biz yapmaya çalışıyoruz tüm gibi görünüyor bana, olan CDAudioManager örneğine kendisine AVAudioSession için temsilci ayarlanır. Bildirimler bunu nasıl gerçekleştirir? Yoksa yukarıdaki kodun amacı hakkında yanlış mıyım?

cevap

17

içine çalıştıran hata uyarısı buna o 2 satırları değiştirmek susturmak için kod

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

bu blokta ise:

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

Umut bu yardımcı olur.

+10

"Neden" bu doğru mu? – Jonny

+0

Apple, iOS 6'da temsilci ve AVAudioSessionDelegate protokolünü ayarlamayı reddetti ve şimdi NSNotification merkezi aracılığıyla bildirimleri dinlemek zorundasınız. – geekinit

+9

Bu cevap eksik görünüyor. –

9

Bu konuda Cocos2D-iPhone.org forumlarında bir angarya buldum. Bunu tam olarak anlamadığım halde - ama üzerinde çalışıyorum - en azından geçici olarak problemi hallediyor gibiydi. bağlantıyı İşte

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

oluyor: 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 
} 

Sonra yerini: bununla

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

Yaptığı CDAudioManger.m dosyasında bu yöntemi yazmaktı

Bu kodun ne yaptığına dair daha iyi bir anlayış geliştirdiğimde, onun yazı.

+0

Ben CDAudioManager yok izler.m – CroiOS

+0

Gösterdiğiniz bağlantı, NSNotification merkezini kullanarak bu yeni formu kullanmayla ilgili sorunlar hakkında da konuşur, iOS 5.0 ile bu satırda bir Kötü Erişim hatası atar ... sooo somut bir çözüm ne olurdu? iOS versiyonunun kontrol edilmesini ve buna göre kod atılmasını önerirler. "Poast" için –

+1

+1. – Jonny

0

Bu test ancak bu yayına uygun değil: 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; 
} 

yani iOS sürümüne bağlı olarak farklı kod çalıştırma zamanı.

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

Ve Başparmaklarımı geçmeye: Sadece ile gidersiniz bu yüzden

Şimdi benim uygulama yalnızca zaten 6.0+ iOS olduğunu. Bunun yerine işlenmeye temsilci kullanım bildirimi kullanmanın

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]; 
} 
İlgili konular