AVAudioSession manipulate sound output

后端 未结 1 1173
北海茫月
北海茫月 2020-12-20 03:03

I\'m using AVSoundSession to configure sound, and AVAudioPlayer to play different sounds. I searched a lot and couldn\'t find anything. How can I

相关标签:
1条回答
  • 2020-12-20 03:19

    So, I found solution for manipulating with sound output.

    You could initialize sound settings with AVAudioSession

    Something like this:

    session = [AVAudioSession sharedInstance];
    
    BOOL success;
    NSError* error;
    
    success = [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
    
    if (!success)  NSLog(@"AVAudioSession error setting category:%@",error);
    success = [session setMode:AVAudioSessionModeVoiceChat error:&error];
    
    if (!success)  NSLog(@"AVAudioSession error setting mode:%@",error);
    
    success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];
    
    [session setPreferredOutputNumberOfChannels:0 error:nil];
    if (!success)  NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error);
    
    success = [session setActive:YES error:&error];
    if (!success) NSLog(@"AVAudioSession error activating: %@",error);
    else NSLog(@"audioSession active");
    

    With

    [session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];
    

    You set don't override output port. And your app playing with default speaker. As I understand for mode AVAudioSessionModeVoiceChat used phone speaker. It's directly what I need for my SIP caller app.

    Then you can override output port with

    [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
    

    I do it so:

    - (void) loudSpeakerOn:(BOOL)isLoudSpeaker{
        [session setActive:NO error:nil];
    
        BOOL success;
        NSError* error;
    
        success = [session overrideOutputAudioPort:isLoudSpeaker?AVAudioSessionPortOverrideSpeaker:AVAudioSessionPortOverrideNone error:&error];
    
        if (!success)  NSLog(@"AVAudioSession error setting category:%@",error);
    
        [session setActive:YES error:nil];
    }
    
    0 讨论(0)
提交回复
热议问题