Home » Android » android – Expo audio recording webm

android – Expo audio recording webm

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am using expo and I am trying to record on android by using the webm output format ( Expo.Audio.RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_WEBM ). My issue is that I have tried all the different encoders and none of them seem to work. I was expecting that the vorbis encoder would work ( Expo.Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_VORBIS ) but I always get back an exception

    {
        "tryLoc": "root",
        "completion": {
            "type": "throw",
            "arg": {
                "framesToPop": 1,
                "code": "E_AUDIO_RECORDING",
                "message": "Start encountered an error: recording not started",
                "stack": "Error: Start encountered an error: recording not started\n    at createErrorFromErrorData (blob:http://172.16.6.80:19001/96c6e3c7-f483-40d9-a3ad-7a7114468a07:2297:17)\n    at blob:http://172.16.6.80:19001/96c6e3c7-f483-40d9-a3ad-7a7114468a07:2249:27\n    at MessageQueue.__invokeCallback (blob:http://172.16.6.80:19001/96c6e3c7-f483-40d9-a3ad-7a7114468a07:2691:18)\n    at blob:http://172.16.6.80:19001/96c6e3c7-f483-40d9-a3ad-7a7114468a07:2436:18\n    at MessageQueue.__guardSafe (blob:http://172.16.6.80:19001/96c6e3c7-f483-40d9-a3ad-7a7114468a07:2604:11)\n    at MessageQueue.invokeCallbackAndReturnFlushedQueue (blob:http://172.16.6.80:19001/96c6e3c7-f483-40d9-a3ad-7a7114468a07:2435:14)\n    at http://172.16.6.80:19001/debugger-ui/debuggerWorker.js:72:58"
            }
        }
    }

This is the configuration that I am trying:

const recording = new Audio.Recording();
debugger;
await recording.prepareToRecordAsync({
  android: {
    extension: '.webm',
    outputFormat: Audio.RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_WEBM,
    audioEncoder: Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_VORBIS,
    sampleRate: 44100,
    numberOfChannels: 2,
    bitRate: 128000
  },
  ios: {
    extension: '.wav',
    audioQuality: Audio.RECORDING_OPTION_IOS_AUDIO_QUALITY_MAX,
    sampleRate: 44100,
    numberOfChannels: 2,
    bitRate: 128000,
    linearPCMBitDepth: 16,
    linearPCMIsBigEndian: false,
    linearPCMIsFloat: false,
  },
});

Any ideas on why that happens and what is the appropriate encoder that I have to use?

How to&Answers:

I am shooting in the dark, hope this may be of help.

Enabling Audio and customizing Audio Mode

Expo.Audio.setIsEnabledAsync(value) Audio is enabled by default, but
if you want to write your own Audio API in a detached app, you might
want to disable the Expo Audio API.

Arguments value (boolean) — true enables Expo Audio, and false
disables it.

Returns A Promise that will reject if audio playback could not be
enabled for the device.

Expo.Audio.setAudioModeAsync(mode) We provide this API to customize
the audio experience on iOS and Android.

These key value pairs may help as well.

  • prepareToRecordAsync()
  • stopAndUnloadAsync()

Audio Encoder list: (*I suggest using the Default just to debug it)

audioEncoder:

  • Expo.Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_DEFAULT

  • Expo.Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AMR_NB

  • Expo.Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AMR_WB

  • Expo.Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AAC

  • Expo.Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_HE_AAC

  • Expo.Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AAC_ELD

  • Expo.Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_VORBIS

Reference: expo.io (13/Feb/2018).

NOTE *.webm (Streamable only in Android 4.0 and above)

Reference: developer.android.com (13/Feb/2018).

Try use below code, high and low quality and android & ios:

  export const RECORDING_OPTIONS_PRESET_HIGH_QUALITY: RecordingOptions = {
  android: {
    extension: '.m4a',
    outputFormat: RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_MPEG_4,
    audioEncoder: RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AAC,
    sampleRate: 44100,
    numberOfChannels: 2,
    bitRate: 128000,
  },
  ios: {
    extension: '.caf',
    audioQuality: RECORDING_OPTION_IOS_AUDIO_QUALITY_MAX,
    sampleRate: 44100,
    numberOfChannels: 2,
    bitRate: 128000,
    linearPCMBitDepth: 16,
    linearPCMIsBigEndian: false,
    linearPCMIsFloat: false,
  },
};

export const RECORDING_OPTIONS_PRESET_LOW_QUALITY: RecordingOptions = {
  android: {
    extension: '.3gp',
    outputFormat: RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_THREE_GPP,
    audioEncoder: RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AMR_NB,
    sampleRate: 44100,
    numberOfChannels: 2,
    bitRate: 128000,
  },
  ios: {
    extension: '.caf',
    audioQuality: RECORDING_OPTION_IOS_AUDIO_QUALITY_MIN,
    sampleRate: 44100,
    numberOfChannels: 2,
    bitRate: 128000,
    linearPCMBitDepth: 16,
    linearPCMIsBigEndian: false,
    linearPCMIsFloat: false,
  },
};

Reference: expo.io (13/Feb/2018).