HTML to APK Builder

ANDROIDAPI REFERENCE

AndroidAPI: record audio and play recordings

Reference for startAudioRecording, stopAudioRecording and getAudioFileBase64: microphone permission, file names, playback in the page and sharing recordings.

Updated:

The audio methods record from the microphone into an .m4a file (AAC audio in an MPEG-4 container) in the app’s cache. Recording is Pro.

Requirements:

  • Declare RECORD_AUDIO in Advanced Build → Permissions.
  • startAudioRecording does not show the permission dialog itself. Request android.permission.RECORD_AUDIO first with requestPermission; without it the result is permission_denied.
Method Event Pro
startAudioRecording audioRecordingStartResult yes
stopAudioRecording audioRecordingResult yes
getAudioFileBase64 audioFileResult no

startAudioRecording

AndroidAPI.startAudioRecording(fileName) // e.g. "note-1.m4a"

Starts recording. fileName must be a simple relative name; use the .m4a extension. Only one recording runs at a time: starting a new one discards the active recording and reports warning: "previous_recording_discarded".

Result event: audioRecordingStartResult {fileName, success, warning, error}. Errors: pro_required, invalid_file_name, permission_denied, start_failed (for example, another app is using the microphone).

window.addEventListener('audioRecordingStartResult', e => {
  const r = e.detail;
  recordButton.disabled = false;
  if (r.success) { recording = true; recordButton.textContent = 'Stop'; }
  else showMessage('Could not start recording: ' + r.error);
});

function start() {
  if (!AndroidAPI.hasPermission('android.permission.RECORD_AUDIO')) {
    return AndroidAPI.requestPermission('android.permission.RECORD_AUDIO', 'mic');
  }
  recordButton.disabled = true;
  AndroidAPI.startAudioRecording('note-' + Date.now() + '.m4a');
}
window.addEventListener('permissionResult', e => {
  if (e.detail.callbackId === 'mic' && e.detail.granted) start();
});

stopAudioRecording

AndroidAPI.stopAudioRecording(callbackId)

Stops the active recording and finishes the file.

Result event: audioRecordingResult {callbackId, success, path, fileName, size, mimeType, error}. mimeType is audio/mp4, size is in bytes and path is the native file path. You can pass path to shareFile, but it is not a URL you can play in the page.

Errors: pro_required, no_active_recording, stop_failed, empty_recording (stopped too quickly to contain audio).

window.addEventListener('audioRecordingResult', e => {
  const r = e.detail;
  recording = false; recordButton.textContent = 'Record';
  if (!r.success) return showMessage('Recording failed: ' + r.error);
  AndroidAPI.getAudioFileBase64(r.fileName, 'play-' + r.fileName);
});
stopButton.onclick = () => AndroidAPI.stopAudioRecording('stop-1');

getAudioFileBase64

AndroidAPI.getAudioFileBase64(fileName, callbackId)

Reads a recording from the cache so you can play or upload it in the page. Files up to 16 MB are supported.

Result event: audioFileResult {callbackId, fileName, success, base64, mimeType, size, error}. base64 is raw base64, not a data URL. Add the prefix yourself before using it as a source. Errors: not_found, invalid_file_name, file_too_large.

window.addEventListener('audioFileResult', e => {
  const r = e.detail;
  if (!r.success) return showMessage(r.error);
  const audio = document.querySelector('audio');
  audio.src = `data:${r.mimeType};base64,${r.base64}`;
  audio.play();
});

Keeping recordings

Recordings are stored in the app’s cache, which Android may clear when storage is low. If recordings must be kept:

  • share or export them with shareFile(path, 'audio/mp4'), or
  • store the base64 in your own storage for small clips, or upload them to your server.

Recording stops when the app is closed. For a browser-only approach, MediaRecorder with getUserMedia({audio: true}) also works when RECORD_AUDIO is declared; it does not need Pro.