Code Monkey home page Code Monkey logo

Comments (3)

thehavre avatar thehavre commented on June 12, 2024 2

Hello, everyone :)

If you are using this example in your app and experiencing issue with speakerphone instead of headset, you need to do some changes in the custom audio driver - OTDefaultAudioDevice.

  1. Remove from audio session setup - setupAudioSession and setupAudioSession:(AVAudioSession *)audioSession setting of default speaker option:
audioOptions |= AVAudioSessionCategoryOptionDefaultToSpeaker;
  1. In same session setup methods change audio session mode from
    Video
[mySession setMode:AVAudioSessionModeVideoChat error:nil];

to Voice

[mySession setMode:AVAudioSessionModeVoiceChat error:nil];
  1. If you need to turn on headset by default (after previous steps it should be chosen by default, but if not...) you can force set it manually by calling
[self configureAudioSessionWithDesiredAudioRoute:AUDIO_DEVICE_HEADSET];

You can do this also in the setupAudioSession or before creation and connection to the OpenTok session.

Here is how setupAudioSession looks like after changes:

- (void) setupAudioSession
{
    AVAudioSession *mySession = [AVAudioSession sharedInstance];
    _previousAVAudioSessionCategory = mySession.category;
    avAudioSessionMode = mySession.mode;
    avAudioSessionPreffSampleRate = mySession.preferredSampleRate;
    avAudioSessionChannels = mySession.inputNumberOfChannels;
    
    [mySession setPreferredSampleRate: kSampleRate error: nil];
    [mySession setPreferredInputNumberOfChannels:1 error:nil];
    [mySession setPreferredIOBufferDuration:kPreferredIOBufferDuration
                                      error:nil];
    
    NSError *error = nil;
    NSUInteger audioOptions = 0;
#if !(TARGET_OS_TV)
    audioOptions |= AVAudioSessionCategoryOptionAllowBluetooth ;
    [mySession setCategory:AVAudioSessionCategoryPlayAndRecord
               withOptions:audioOptions
                     error:&error];
#else
    [mySession setCategory:AVAudioSessionCategoryPlayback
               withOptions:audioOptions
                     error:&error];
#endif
    
    [mySession setMode:AVAudioSessionModeVoiceChat error:nil];
    
    if (error)
        OT_AUDIO_DEBUG(@"Audiosession setCategory %@",error);
    
    error = nil;
    
    [self setupListenerBlocks];
    [self configureAudioSessionWithDesiredAudioRoute:AUDIO_DEVICE_HEADSET];
    [mySession setActive:YES error:&error];
    
    if (error)
        OT_AUDIO_DEBUG(@"Audiosession setActive %@",error);
    
}
  1. Don't forget to pre-heat audio session before starting CallKit's AudioSession as TokBox provided in example - in CXStartCallAction and CXAnswerCallAction

Minor explanation why it's all happening with us :)

  1. When Audio Session configured with AVAudioSessionCategoryOptionDefaultToSpeaker it's obviously uses speaker by default :)
  2. When you choose AVAudioSessionModeVideoChat mode, session obviously (for Apple :D) uses speaker by default and you can't switch because... it's Apple's logic, I suppose.
  3. And if session is configured with AVAudioSessionModeVoiceChat mode, iOS and CallKit behaves as voice call with ability yo switch between speakers (and also turning screen off when user brings phone to ear).

BTW, from Apples documentation:

/* Only valid with kAudioSessionCategory_PlayAndRecord. Reduces the number of allowable audio
routes to be only those that are appropriate for video chat applications. May engage appropriate
system-supplied signal processing.  **Has the side effect of setting
AVAudioSessionCategoryOptionAllowBluetooth and AVAudioSessionCategoryOptionDefaultToSpeaker.** */
AVF_EXPORT AVAudioSessionMode const AVAudioSessionModeVideoChat API_AVAILABLE(ios(7.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);

And also, from documentation, Apple suggests to use AVAudioSessionModeVoiceChat for both audio and video calls.

from callkit.

Lucashuang0802 avatar Lucashuang0802 commented on June 12, 2024

@mrazam110 thanks for this concise report. We'll work on that change.

from callkit.

alexbaramilis avatar alexbaramilis commented on June 12, 2024

Hi, any updates on this?

from callkit.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.