iOS
This part of guide describes best practices and common development pitfalls when implementing IDVoice-SDK-based products for iOS-based devices, such as iPhones or iPads.
Since Release 3.2.1 we're distributing iOS libraries with bitcode enabled. I.e., despite being slightly bigger, these libraries will actually take less space and are better optimized when incorporated into real applications.
Recording audio in iOS is relatively simple but there are some aspects that need to be taken into consideration in order to achieve the most efficient compatibility with ID R&D's IDVoice SDK.
Working with audio is done via AVFoundation
framework using AVAudioEngine
for processing, AVAudioSession
for system communication and AVAudioRecorder
for recording capabilities.
There are three main points that developers have to take care off to set up audio handling properly.
Set audio session category and mode¶
In this case correct options would be playAndRecord category and default mode.
- playAndRecord — this category is used for recording (input) and playback (output) of audio.
- default — this mode is used to minimize the amount of system-supplied signal processing to input signal.
Note
This is especially important for the correct work of IDVoice SDK Liveness check
The audio session's category and mode together define how your app uses audio. Typically, you set the category and mode before activating the session.
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default)`
try AVAudioSession.sharedInstance().setActive(true)
Get access to the audio data on the output bus¶
This is done via installTap(onBus:bufferSize:format:block:)
instance method. For this method to work it's necessary to:
- determine device's hardware sample rate
let audioEngine: AVAudioEngine = AVAudioEngine()
let sampleRate = audioEngine.inputNode.inputFormat(forBus: 0).sampleRate
- initialize an audio format instance with hardware sample rate
let inputFormat = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: sampleRate, channels: 1, interleaved: false)
- install an audio tap on the bus to record, monitor, and observe the output of the node
audioEngine.inputNode.installTap(onBus: 0, bufferSize: 4096, format: inputFormat, block: bufferClosure)`
Set recording settings for speech with maximum quality and initialize recorder¶
let recordSettings = [
AVFormatIDKey: kAudioFormatLinearPCM,
AVSampleRateKey: sampleRate,
AVNumberOfChannelsKey: 1,
AVLinearPCMBitDepthKey: 16,
AVEncoderAudioQualityKey: AVAudioQuality.max.rawValue
] as [String : Any]
try audioRecorder = AVAudioRecorder(url: audioFileURL, settings: recordSettings)
Useful resources¶
As a first step in implementing your own iOS application with IDVoice SDK you can take a look at the basic iOS Recorder Example that we have available for sharing (MIT license).