This plugin provides the ability to read raw audio data from smartphone, benefiting for the APP development using ionic framework.
NOTE: The current implementation only support Android. A future implementation will support iOS. Our plugin is different from
cordova-plugin-media
: This plugin stores the data in a file instead of reporting to upper layer, while our plugin provides more low-level and direct access to low-level raw audio data.cordova-plugin-audio-recorder-api
: We borrow the basic idea from this plugin.However, this plugin still stores the audio data in a file.
- Ionic + Andriod
cordova plugin add cordova-plugin-audio-reader
This plugin defines a gloabl variable window.plugins.audioReader
. Despite in the global scope, it is not available until after the deviceready event.
document.addEventListener('deviceready', function(){
window.plugins.audioReader.init();
}, false)
A quick example is given as follows:
var config = {
source: AudioSource.DEFAULT,
channel: AudioChannel.CHANNEL_IN_MONO,
format: AudioFormat.ENCODING_PCM_16BIT,
sampleRate:44100 // sample frequency
}
$scope.start = function(){
windows.plugins.audioReader.init(config);
windows.plugins.audioReader.start();
}
$scope.stop = function(){
windows.plugins.audioReader.stop();
}
$scope.record = function(){
var succes = function(result){
var leftChannel = result.leftChannel;
var rightChannel = result.rightChannel;
//handle the data read from left or right channel.
....
}
setInterval(function(){
windows.plugins.audioReader.read(44100,0,success,null);
},1000)
}
The following constants are used for configuration
Audio Source
- AudioSouce.DEFAULT = 0
- AudioSource.MIC = 1
Audio Channel
- AudioChannel.CHANNEL_IN_MONO = 16
- AudioChannel.CHANNEL_STEREO = 12
Audio Format
- AudioFormat.ENCODING_PCM_16BIT = 2
- AudioFormat.ENCODING_PCM_8BIT = 3
- AudioFormat.ENCODING_PCM_FLOAT = 4
Default configuration:
var defaultConfig = {
source: AudioSource.DEFAULT,
channel: AudioChannel.CHANNEL_IN_MONO,
format: AudioChannel.ENCODING_PCM_16BIT,
sampleRate:44100
}
You could define your own configuration as a parameter of init
function.
document.addEventListener('deviceready', function(){
window.plugins.audioReader.init(yourConfig);
}, false)
audioReader.init
: Initailizes theaudioReader
with customized configuration.audioReader.start
: Starts the recording.audioReader.stop
: Stops the recording.audioReader.read
: Reads the raw data from audio channels.audioReader.clear
: Clears the raw data from buffers.
Initializes the audioReader
audioReader.init(config, successCallback, errorCallback)
Starts the recording. Note that all the raw data are stored in buffer.
audioReader.start(successCallback, errorCallback)
Stops the recording. NOTE, the audioReader should be re-initialized after the stop is called.
audioReader.stop(successCallback, errorCallback)
Clears the data stored in the buffers.
audioReader.clear()
Reads raw audio data from buffers. in buffer.
audioReader.read(length, channel, successCallback, errorCallback)
Parameters:
- length: how many data do you want to read.
- channel: which channel do you want to read from. 0 - both, 1- left channel, 2- right channel.
- successCallback: the passed parameter contains two variables:
leftChannel
andrightChannel
For example:
audioReader.read(44100, 0, function(result){
//return an array including audio data read from left channel.
var leftArray = result.leftChannel;
//reutrn an array including audio data read from right channel.
var rightArray = result.rightChannel;
})