2014-10-01 15 views
5

Yakınlık algılayıcısı 1'e bildirdiğinde Ses Çıkışı'nı Hoparlör'den Alıcı/Kulaklık'a (kulaklığın bağlı olup olmamasına bakılmaksızın) değiştirme seçeneği olan bir ses çalara sahibim. iOS7 ve üzeri sürümlerde Alıcı ve Hoparlör arasında ses çıkışı mı değiştiriliyor?

- (void) switchAudioOutput:(NSString*)output{ 
    AVAudioSession* audioSession = [AVAudioSession sharedInstance]; 
    BOOL success; 
    NSError* error; 

    if([output isEqualToString:keAudioOutputReciever]){ 
     //Force current audio out through reciever 
     //set the audioSession override 
     success = [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideNone 
              error:&error]; 
     if (!success) 
      NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error); 

     //activate the audio session 
     success = [audioSession setActive:YES error:&error]; 
     if (!success) 
      NSLog(@"AVAudioSession error activating: %@",error); 
     else 
      NSLog(@"AVAudioSession active with override: AVAudioSessionPortOverrideNone"); 

    }else if([output isEqualToString:keAudioOutputSpeaker]){ 
     //set the audioSession override 
     success = [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker 
                error:&error]; 
     if (!success) 
      NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error); 

     //activate the audio session 
     success = [audioSession setActive:YES error:&error]; 
     if (!success) 
      NSLog(@"AVAudioSession error activating: %@",error); 
     else 
      NSLog(@"AVAudioSession active with override: AVAudioSessionPortOverrideSpeaker"); 

    } 
} 

Bu

cevap Toggle Button route audio to speaker and receiver ve enter link description here dayanıyordu. Bunun sadece sesi yalnızca hoparlöre zorladığını, ancak rotanın sadece alıcıya gitmesini sağladığını fark ettim. Üstelik hoparlöre kayması sırasında aşağıdaki hatayı alıyorum:

AVAudioSession error overrideOutputAudioPort:Error Domain=NSOSStatusErrorDomain Code=-50 "The operation couldn’t be completed. (OSStatus error -50.)"

+1

geçersiz kılmaları kaçınarak cevabını anladım. Birisi bunu çözmeyi başardı mı? –

cevap

7

Ben de tam aynı hatayı geting ediyorum Alıcı

için
- (void) setAudioSession:(NSString*)audioOutput{ 

     NSError* error; 
     if([audioOutput isEqualToString:audioOutputSpeaker.lowercaseString]){ 

      //set the audioSession override 
      if(![self setCategory:AVAudioSessionCategoryPlayAndRecord 
           withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionAllowBluetooth 
            error:&error]) 
       NSLog(@"AVAudioSession error AVAudioSessionCategoryPlayAndRecord:%@",error); 

      //activate the audio session 
      if (![self setActive:YES error:&error]) 
       NSLog(@"AVAudioSession error activating: %@",error); 
      else 
       NSLog(@"AVAudioSession active with override: AVAudioSessionPortOverrideNone"); 
     }else if ([audioOutput isEqualToString:audioOutputReciever.lowercaseString]){ 
      //Force current audio out through reciever 
      //set the audioSession override 
      if(![self setCategory:AVAudioSessionCategoryPlayAndRecord 
           withOptions:AVAudioSessionCategoryOptionAllowBluetooth 
            error:&error]) 
       NSLog(@"AVAudioSession error AVAudioSessionCategoryPlayAndRecord:%@",error); 

      if (![self overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error]) 
       NSLog(@"AVAudioSession error overrideOutputAudioPort to Reciever:%@",error); 

      //activate the audio session 
      if (![self setActive:YES error:&error]) 
       NSLog(@"AVAudioSession error activating: %@",error); 
      else 
       NSLog(@"AVAudioSession active with override: AVAudioSessionPortOverrideNone"); 
     } 
    } 
İlgili konular