titanium-sdk
titanium-sdk copied to clipboard
feat(android): audio format parity for AudioRecorder
work in progress
Extends parity for AudioRecorder:
- add file format parity: AAC, MP4, WAVE, 3GPP
- add compressions: AUDIO_FORMAT_LINEAR_PCM, AUDIO_FORMAT_AAC ,AUDIO_FORMAT_VORBIS, AUDIO_FORMAT_HE_AAC, AUDIO_FORMAT_AMR_NB, AUDIO_FORMAT_AMR_WB, AUDIO_FORMAT_AAC_ELD
- fix example in README
example:
const window = Ti.UI.createWindow({
layout: "vertical"
});
const btnOptions1 = Ti.UI.createButton({
title: 'AAC',
});
const btnOptions2 = Ti.UI.createButton({
title: 'Default',
});
const recordStart = Ti.UI.createButton({
title: 'Start',
top: 40
});
const recordPause = Ti.UI.createButton({
title: 'Pause',
});
const recordStop = Ti.UI.createButton({
title: 'Stop',
});
const recordPlay = Ti.UI.createButton({
title: 'Play',
});
const lbl = Ti.UI.createLabel({
text: "-",
left: 20,
right: 20
});
const optionsFormat = Ti.UI.createOptionDialog({
options: ['WAVE', 'AAC', 'MP4', '3GP']
});
var audioFormat = Ti.Media.AUDIO_FILEFORMAT_AAC;
optionsFormat.addEventListener("click", e => {
btnOptions1.title = optionsFormat.options[e.index];
if (e.index == 0) {
audioFormat = Ti.Media.AUDIO_FILEFORMAT_WAVE;
} else if (e.index == 1) {
audioFormat = Ti.Media.AUDIO_FILEFORMAT_AAC;
} else if (e.index == 2) {
audioFormat = Ti.Media.AUDIO_FILEFORMAT_MP4;
} else if (e.index == 3) {
audioFormat = Ti.Media.AUDIO_FILEFORMAT_3GPP;
}
});
const optionsCompression = Ti.UI.createOptionDialog({
options: ['DEFAULT', 'AAC', 'VORBIS', 'HE_AAC', 'AMR_NB', 'AMR_WB', 'AAC_ELD']
});
var audioCompression = Ti.Media.AUDIO_FORMAT_AAC;
optionsCompression.addEventListener("click", e => {
btnOptions2.title = optionsCompression.options[e.index];
if (e.index == 0) {
audioCompression = Ti.Media.AUDIO_FORMAT_LINEAR_PCM;
} else if (e.index ==1) {
audioCompression = Ti.Media.AUDIO_FORMAT_AAC;
} else if (e.index == 2) {
audioCompression = Ti.Media.AUDIO_FORMAT_VORBIS;
} else if (e.index == 3) {
audioCompression = Ti.Media.AUDIO_FORMAT_HE_AAC;
} else if (e.index == 4) {
audioCompression = Ti.Media.AUDIO_FORMAT_AMR_NB;
} else if (e.index == 5) {
audioCompression = Ti.Media.AUDIO_FORMAT_AMR_WB;
} else if (e.index == 6) {
audioCompression = Ti.Media.AUDIO_FORMAT_AAC_ELD;
}
});
const audioRecorder = Ti.Media.createAudioRecorder({
format: audioFormat,
compression: audioCompression
});
var record;
var audioPlayer;
window.addEventListener('open', function(e) {
if (!Ti.Media.hasAudioRecorderPermissions()) {
Ti.Media.requestAudioRecorderPermissions(function(e) {
if (e.success) {
window.add([btnOptions1, btnOptions2, recordStart, recordPause, recordStop, recordPlay, lbl]);
} else {
alert("please allow audio permissions");
}
});
} else {
window.add([btnOptions1, btnOptions2, recordStart, recordPause, recordStop, recordPlay, lbl]);
}
});
btnOptions1.addEventListener('click', function(e) {
optionsFormat.show();
});
btnOptions2.addEventListener('click', function(e) {
optionsCompression.show();
});
recordStart.addEventListener('click', function(e) {
audioRecorder.format = audioFormat;
audioRecorder.compression = audioCompression;
audioRecorder.start();
});
recordPause.addEventListener('click', function(e) {
if (audioRecorder.paused) {
recordPause.title = 'Pause';
audioRecorder.resume()
} else {
recordPause.title = 'Resume';
audioRecorder.pause();
}
});
recordStop.addEventListener('click', function(e) {
record = audioRecorder.stop();
Ti.API.info(record.nativePath);
lbl.text = record.nativePath + "\nSize: " + record.size;
});
recordPlay.addEventListener('click', function(e) {
audioPlayer = Ti.Media.createAudioPlayer({
url: record.nativePath
});
audioPlayer.start();
});
window.open();
WARNING: Not all "devices" will support the audio format constant selected. In my experience working on Android for about 10 years, I've literally seen all of these constants fail on various devices. 3GP is the most widely supported format (since there is no royalties/licensing involved), but I have seen 3GP fail to work on Barnes & Noble's Nook devices before (it's a dead product line now, but proves my point). This is why Titanium's audio recorder was limited to uncompressed WAV/PCM, because it's the only format guaranteed to work on ALL devices.