2014-07-06 5 views
7

Sto provando a reindirizzare l'audio agli altoparlanti nello AppRTC iOS example.Come reindirizzare l'audio agli altoparlanti nell'esempio AppRTC iOS?

ho provato:

AVAudioSession* session = [AVAudioSession sharedInstance]; 

//error handling 
BOOL success; 
NSError* error; 

//set the audioSession category. 
//Needs to be Record or PlayAndRecord to use audioRouteOverride: 

success = [session setCategory:AVAudioSessionCategoryPlayAndRecord 
         error:&error]; 

if (!success) NSLog(@"AVAudioSession error setting category:%@",error); 

//set the audioSession override 
success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker 
             error:&error]; 
if (!success) NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error); 

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

non ci sono errori, ma non funziona. Come posso risolvere questo?

+0

Hai trovato qualche soluzione per il tuo problema? Sto affrontando lo stesso problema di te al momento. –

+1

@JosipB. https://github.com/alongubkin/audiotoggle –

+0

non funziona per me ... –

risposta

0

Ho trovato la soluzione alla fine.

La ragione era che è necessario impostare la categoria AVAudioSession su AVAudioSessionCategoryPlayback. Ma per qualche motivo dopo aver stabilito la chiamata webRTC è stato impostato su AVAudioSessionCategoryPlayAndRecord. Alla fine ho deciso di aggiungere Observer per AVAudioSessionRouteChangeNotification e passare a AVAudioSessionCategoryPlayback ogni volta che ho rilevato una modifica di categoria indesiderata. Un po 'di soluzione di hacking ma alla fine ha funzionato. Puoi verificarlo here.

9

L'ho risolto con la soluzione. Basta ascoltare AVAudioSessionRouteChangeNotification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSessionRouteChange:) name:AVAudioSessionRouteChangeNotification object:nil]; 

ed usando il selettore didSessionRouteChange come di seguito:

- (void)didSessionRouteChange:(NSNotification *)notification 
{ 
    NSDictionary *interuptionDict = notification.userInfo; 
    NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue]; 

    switch (routeChangeReason) { 
     case AVAudioSessionRouteChangeReasonCategoryChange: { 
      // Set speaker as default route 
      NSError* error; 
      [[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error]; 
     } 
     break; 

    default: 
     break; 
    } 
} 
0

phuongle's answer è corretto. Tuttavia, quando abiliti l'override, in realtà l'override dell'output audio si verifica anche quando l'utente inserisce le cuffie. Non ha molto senso riprodurre l'audio attraverso l'altoparlante quando l'utente usa le cuffie. A tale scopo utilizzare il seguente codice:

- (void)didSessionRouteChange:(NSNotification *)notification 
{ 
    NSDictionary *interuptionDict = notification.userInfo; 
    const NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue]; 

    if (routeChangeReason == AVAudioSessionRouteChangeReasonRouteConfigurationChange) { 
     [self enableLoudspeaker]; 
    } 
} 

- (void)enableLoudspeaker { 
    AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
    AVAudioSessionCategoryOptions options = audioSession.categoryOptions; 
    if (options & AVAudioSessionCategoryOptionDefaultToSpeaker) return; 
    options |= AVAudioSessionCategoryOptionDefaultToSpeaker; 
    [audioSession setActive:YES error:nil]; 
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:options error:nil]; 
} 
Problemi correlati