TVODefaultAudioDevice
Objective-C
@interface TVODefaultAudioDevice : NSObject <TVOAudioDevice>
Swift
class TVODefaultAudioDevice : NSObject, TVOAudioDevice
TVODefaultAudioDevice allows you to record and playback audio when you are connected to a Call.
-
@brief A block to configure the AVAudiosession.
@discussion When you connect to or accept a Call, the SDK executes this block. If CallKit is not in use (the connect or accept options have no
uuid), the SDK first activates the audio session. To reconfigure the session after connecting, for example to change the audio route, execute the block yourself. Please note that the getter of this property returns a wrapper block which internally executes the block you set. By default, the SDK returns a default wrapper block which executeskTVODefaultAVAudioSessionConfigurationBlockinternally.Avoid executing the block on the main thread. It configures AVAudioSession, and changing the category of an active session on the main thread can lead to UI unresponsiveness. Dispatch the call to a serial queue that your application owns, as shown below, so that successive reconfigurations are applied in order.
@important The SDK invokes this block on its own serial background queue, not on the thread that called
connectWithOptions:delegate:oracceptWithOptions:delegate:. Do not touch UIKit, read main-actor isolated state or assertNSThread.isMainThreadfrom inside the block. The work is also asynchronous: it completes afterconnectWithOptions:delegate:/acceptWithOptions:delegate:has returned.The following example demonstrates changing the audio route from speaker to receiver:
TVODefaultAudioDevice *audioDevice = [TVODefaultAudioDevice audioDevice];
TwilioVoiceSDK.audioDevice = audioDevice;
// Created once, for example alongside the audio device. dispatch_queue_t audioSessionQueue = dispatch_queue_create(“com.example.audiosession”, DISPATCH_QUEUE_SERIAL);
//…connect to a Call with audioDevice. By default the audio route will be configured to receiver.
audioDevice.block = ^ { // We will execute
kTVODefaultAVAudioSessionConfigurationBlockfirst. kTVODefaultAVAudioSessionConfigurationBlock();// Overwrite the audio route AVAudioSession *session = [AVAudioSession sharedInstance]; NSError *error = nil; if (![session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error]) { NSLog(@"AVAudiosession overrideOutputAudioPort %@",error); }};
dispatch_async(audioSessionQueue, ^{ audioDevice.block(); });
Declaration
Objective-C
@property (nonatomic, copy, nonnull) TVOAVAudioSessionConfigurationBlock block;Swift
var block: TVOAVAudioSessionConfigurationBlock { get set } -
@brief A boolean to ignore preferred configuration errors.
@discussion When YES, calls to AudioSession preferred configuration ignore errors in configuring the audio session’s “preferred” attributes (e.g. preferredInputNumberOfChannels). Typically, configurations to preferred attributes are optimizations, and ignoring this type of configuration error allows code flow to continue along the happy path when these optimization are not available. The default value of this property is NO.
Declaration
Objective-C
@property (class, nonatomic) BOOL ignoresPreferredAttributeConfigurationErrors;Swift
class var ignoresPreferredAttributeConfigurationErrors: Bool { get set } -
@brief A boolean which enables playback and recording.
@discussion By default, this property is
NO. Connecting to or accepting a Call without CallKit enables it. Setting it toNOstops the underlying CoreAudio graph (if active) and prevents it from being started again. Setting it toYESallows the audio graph to be started whenever there is audio to play or record in a Call. The change is applied asynchronously on an internal queue;isEnabledreports the value you most recently set, which may not have taken effect yet.Declaration
Objective-C
@property (nonatomic, getter=isEnabled) BOOL enabled;Swift
var isEnabled: Bool { get set } -
Unavailable
Use
audioDeviceto create aTVODefaultAudioDevice.@brief Developers shouldn’t initialize this class directly.
@discussion Use
defaultAudioDeviceto create aTVODefaultAudioDevice.Declaration
Objective-C
- (null_unspecified instancetype)init; -
@brief Factory method to create an instance of
TVODefaultAudioDevice.Declaration
Objective-C
+ (nonnull instancetype)audioDevice;Swift
convenience init() -
@brief Factory method with a block to create an instance of
TVODefaultAudioDevice.@discussion Use this factory method if you want to connect to a Call with your choice of audio session configuration.
The following example demonstrates connecting to a Call using the AVAudioSessionCategoryPlayback category:
TwilioVoiceSDK.audioDevice = [TVODefaultAudioDevice audioDeviceWithBlock:^ { // We will execute
kTVODefaultAVAudioSessionConfigurationBlockfirst. kTVODefaultAVAudioSessionConfigurationBlock();// Overwrite the category to `playback` AVAudioSession *session = [AVAudioSession sharedInstance]; NSError *error = nil; if (![session setCategory:AVAudioSessionCategoryPlayback mode:AVAudioSessionModeVoiceChat options:AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionAllowBluetoothA2DP error:&error]) { NSLog(@"AVAudioSession setCategory:options:mode:error: %@",error); }}];
TVOConnectOptions *connectOptions = [TVOConnectOptions optionsWithToken:token block:^(TVOConnectOptionsBuilder *builder) { // configure builder attributes here }];
TVOCall *call = [TwilioVoiceSDK connectWithOptions:connectOptions];
Declaration
Objective-C
+ (nonnull instancetype)audioDeviceWithBlock: (TVOAVAudioSessionConfigurationBlock _Nonnull)block;Swift
convenience init(block: @escaping TVOAVAudioSessionConfigurationBlock)Parameters
blockThe AVAudioSession configuration block.
Return Value
An instance of TVODefaultAudioDevice.