DariusL
DariusL

Reputation: 4077

Android MediaRecorder error(1, -2147483648)

I'm using a MediaRecorder to record audio and save it to a .mp4 with AAC audio. Everything works fine on all the devices I've tried except the Nexus S with Android 4.1. On those devices I either get an error(1, -2147483648) on start() (I think) or it continues fine, but the output file is always empty. I have the necessary permissions, as the app works on other devices.

mRecorder.reset();
mRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {

        @Override
        public void onError(MediaRecorder mr, int what, int extra) {
            Log.e("sagasg", what + "   "   + extra);

        }
    });
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    mRecorder.setAudioSamplingRate(SAMPLING_RATE);
    mRecorder.setAudioEncodingBitRate(BIT_RATE);
    mFileName = "unnamed-" + mTimeStarted.year + "-" + mTimeStarted.month + "-" + mTimeStarted.monthDay
            + "-" + mTimeStarted.hour + "-" + mTimeStarted.minute + "-" + mTimeStarted.second;

    mRecorder.setOutputFile(mFilePath + mFileName + mExtension);

    try {
        mRecorder.prepare();
    } catch (IOException e) {
        Log.e(LOG_TAG, "prepare() failed");
    }

    mRecorder.start();

Upvotes: 2

Views: 2207

Answers (2)

DariusL
DariusL

Reputation: 4077

I've solved it by making random changes to the settings. It started working when I removed

mRecorder.setAudioSamplingRate(SAMPLING_RATE);

Where sampling rate is

public static final int SAMPLING_RATE = 48000;

According to the documentation AAC supports 8-48 kHz, but for some reason it doesn't. Now I just have to fix yet another bug which only appears on the Nexus S. Now I understand why developers prefer iOS.

Edit: now it doesn't crash because of this options, just the recording is an empty file. Tried other values, like, 24000. Same result. I'll have to stick with the default sampling rate.

Upvotes: 4

Cat
Cat

Reputation: 67522

These errors are a pain to debug, but I think this'll do the trick.

This is the line I suggest you change:

mRecorder.setOutputFile(mFilePath + mFileName + mExtension);

You should either use a FileInputStream:

FileInputStream inputStream = new FileInputStream(mFilePath + mFileName + mExtension);
mRecorder.setDataSource(inputStream.getFD());

Or you may also try a FileOutputStream:

FileOutputStream outputStream = new FileOutputStream(mFilePath + mFileName + mExtension);
mRecorder.setOutputFile(outputStream.getFD());

Note that, for either of these solutions, you may have to catch one or more of IllegalArgumentException, SecurityException, IllegalStateException, and IOException.

Upvotes: 2

Related Questions