0

I'm making a voice and video chat app for iOS using OpenTok. The audio for video streams AND audio-only streams works find when I have a headset plugged in. If I don't, it plays through the speakerphone. How do I change this?

spnkr
  • 952
  • 9
  • 18
  • Anyone had any luck with [OTAudioDeviceManager setAudioDevice:] or the OTAudioBus class? – spnkr Dec 30 '17 at 03:33

1 Answers1

2

Setting up the audio device and the audio bus

[OTAudioDeviceManager setAudioDevice:[[OTKBasicAudioDevice alloc] init]];

Use the OTAudioFormat class, defined in the OpenTok iOS SDK, to define the audio format used by the custom audio driver. The [OTKBasicAudioDevice init] method creates an instance of the OTAudioFormat class, and sets the sample rate and number of channels for the audio format:

- (id)init
{
    self = [super init];
    if (self) {
        self = [super init];
        if (self) {
            _otAudioFormat = [[OTAudioFormat alloc] init];
            _otAudioFormat.sampleRate = kSampleRate;
            _otAudioFormat.numChannels = 1;
        }

        // ...
    }
    return self;
}

The init method also sets up some local properties that report whether the device is capturing, whether capturing has been initialized, whether it is rendering and whether rendering has been initialized:

_isDeviceCapturing = NO;
_isCaptureInitialized = NO;
_isDeviceRendering = NO;
_isRenderingInitialized = NO;

The init method also sets up a file to save the incoming audio to a file. This is done simply to illustrate a use of the custom audio driver's audio renderer:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask,
                                                     YES);
NSString *path = [paths[0] stringByAppendingPathComponent:kOutputFileSampleName];

[[NSFileManager defaultManager] createFileAtPath:path
                                        contents:nil
                                      attributes:nil];
_outFile = [NSFileHandle fileHandleForReadingAtPath:path];

The [OTKBasicAudioDevice setAudioBus:] method (defined by the OTAudioDevice protocol) sets the audio bus to be used by the audio device (defined by the OTAudioBus protocol). The audio device uses this object to send and receive audio samples to and from a session. This instance of the object is retained for the lifetime of the implementing object. The publisher will access the OTAudioBus object to obtain the audio samples. And subscribers will send audio samples (from subscribed streams) to the OTAudioBus object. Here is the OTKBasicAudioDevice implementation of the [OTAudioDevice setAudioBus:] method:

- (BOOL)setAudioBus:(id<OTAudioBus>)audioBus
{
    self.otAudioBus = audioBus;
    return YES;
}

The [OTKBasicAudioDevice setAudioBus:] method (defined by the OTAudioDevice protocol) method sets the audio rendering format, the OTAudioFormat instance that was created in the the init method:

- (OTAudioFormat*)renderFormat
{
    return self.otAudioFormat;
}

For More Referece use this.

Gangani Roshan
  • 583
  • 1
  • 8
  • 25
  • Is there an audio driver that sends audio through the phone's small phone speaker? Like the speaker that you use on normal calls? – spnkr Jan 05 '18 at 22:09
  • @spnkr it's use for to set a custom audio device to be used by the app. The audio device manages access to the audio capturing and rendering hardware. for more detail https://tokbox.com/developer/sdks/ios/reference/Classes/OTAudioDeviceManager.html – Gangani Roshan Jan 06 '18 at 04:45
  • @spnkr i would answer based on your question. – Gangani Roshan Jan 06 '18 at 04:46