2012-10-25 5 views
11

Rozpoczęto tworzenie szablonu Cocos2D 2.1 (bez silnika fizyki) w Xcode 4.5, przeznaczonego dla iOS 6 i iPada. W pliku CDAudioManager.m poniższy kod ...Cocos2D 2.1: "Delegat" przestarzały w iOS 6. Jak ustawić delegata dla tej AVAudioSession?

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

... generuje następujące ostrzeżenie ...

"delegate deprecated: first deprecated in iOS 6" 

Więc idę do dokumentacji jabłoni dewelopera, i mówi pod "delegate", "Przestarzałe w iOS 6.0. Zamiast tego użyj powiadomień opisanych w sekcji Powiadomienia tej klasy."

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

Problem jest, wygląda mi na to wszystko staramy się zrobić - wybacz mój brak doświadczenia - jest ustawiony delegata dla AVAudioSession na przykład samego CDAudioManager. W jaki sposób powiadomienia realizują to? Czy po prostu mylę się co do celu powyższego kodu?

Odpowiedz

17

Błąd używasz do jest w tym bloku kodu

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

Aby wyciszyć zmienić te 2 linie to ostrzeżenie:

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

Nadzieja to pomaga.

+10

"Dlaczego" jest prawidłowe? – Jonny

+0

Apple wycofał ustawienie delegata i protokołu AVAudioSessionDelegate w systemie iOS 6, a teraz trzeba wysłuchać powiadomień za pośrednictwem centrum NSNotification. – geekinit

+9

Ta odpowiedź wygląda na niekompletną. –

9

Znalazłem o tym na forum Cocos2D-iPhone.org. Chociaż nie w pełni to rozumiem - ale pracuję nad tym - wydaje mi się, że zajmował się on problemem, przynajmniej tymczasowo. To, co zrobił było napisać tę metodę w pliku 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 
} 

Potem zastąpić:

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

z tym:

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

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

Jeśli i kiedy lepiej zrozumiem, co ten kod robi, z pewnością dokonam edycji jego post.

+0

nie mam CDAudioManager.m – CroiOS

+0

Link, który pokazałeś, również mówi o problemach związanych z używaniem nowego formularza centrum NSNotification, zgłasza błąd Bad Access w tej linii z iOS 5.0 ... tak, co byłoby konkretnym rozwiązaniem? sugerują, aby sprawdzić wersję systemu iOS i podać kod rzutu. –

+1

+1 dla "poast". – Jonny

0

Nie testowałem tego, ale w zależności od tego postu: 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; 
} 

Tj rzucać różne środowisko uruchomieniowe kodu w zależności od wersji iOS.

Teraz moja aplikacja jest iOS 6.0+ tylko tak więc ja po prostu iść z:

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

I krzyż moje kciuki.

10

Zamiast korzystać z powiadomień delegata do obsługi następująco

[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]; 
} 
Powiązane problemy