Last active
June 21, 2019 19:47
-
-
Save liz-ete/119ac00c91334d3951601cf347bad8d4 to your computer and use it in GitHub Desktop.
Use of Android's MediaRecorder with Titanium Hyperloop to record audio.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Please refer to Android's MediaRecorder documentation: | |
// https://developer.android.com/reference/android/media/MediaRecorder | |
var MediaRecorder = require('android.media.MediaRecorder'); | |
var recorder = new MediaRecorder(); | |
function recordAudio() { | |
var recordPermission = "android.permission.RECORD_AUDIO"; | |
var hasRecordPermission = Ti.Android.hasPermission(recordPermission); | |
var permissionsToRequest = []; | |
if (!hasRecordPermission) { | |
permissionsToRequest.push(recordPermission); | |
} | |
if (permissionsToRequest.length > 0) { | |
Ti.Android.requestPermissions(permissionsToRequest, function(e) { | |
if (e.success) { | |
console.log("SUCCESS"); | |
} else { | |
console.log("ERROR: " + e.error); | |
} | |
}); | |
} | |
var recordingsDir = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'recordings'); | |
if (!recordingsDir.exists()) { | |
console.log('create dir') | |
recordingsDir.createDirectory(); | |
} | |
console.log(recordingsDir.exists()); | |
var recordingPathFile = recordingsDir.resolve(); | |
var recordingPath = recordingPathFile.slice(7) + '/testfile.m4a'; | |
console.log(recordingPath); | |
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); | |
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); | |
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); | |
recorder.setOutputFile(recordingPath); | |
recorder.prepare(); | |
recorder.start(); | |
} | |
$.index.open(); | |
$.start.addEventListener('click', recordAudio); | |
$.stop.addEventListener('click', function(e){ | |
recorder.stop(); | |
console.log('stop'); | |
recorder.reset(); // You can reuse the object by going back to setAudioSource() step | |
recorder.release(); // Now the object cannot be reused | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Alloy> | |
<Window class="container"> | |
<Label id="start">Start Recording</Label> | |
<Label id="stop">Stop</Label> | |
</Window> | |
</Alloy> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment