1

I want to record voice in pure audio format. currently I am using this code

public static void startRecording(String fileName) {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        String mFileName = Environment.getExternalStorageDirectory()
                .getAbsolutePath();
        mFileName += "/abc.mp3";
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        try {
            mRecorder.prepare();
            mRecorder.start();
        } catch (IOException e) {
            Log.e(TAG, "prepare() failed");
        }
    }

This is showing the file as mp3 but the internal file content mime type is "video/mp4". What I need is any audio mime type.

EDIT1 As suggested by Headuck, I changed my code to

mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
mFileName += "/abc.aac";
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

The audio got recorded perfectly with "audio/x-hx-aac-adts" mime type. Thanks Headuck.

However my problem is:

I am uploading this file to Google Drive using Cloud endpoints. Then when I open the file it is saying "Oops..! There was a problem in playing this file". I need the file to be played as "MP3" files play.

Does Google Drive supports "ACC" format?. If not please suggest alternatives.
Please help.

Chandan Reddy
  • 499
  • 4
  • 23
  • Have you consulted the documentation on `setOutputFormat()` and http://developer.android.com/reference/android/media/MediaRecorder.OutputFormat.html ? – headuck Sep 16 '15 at 09:38
  • thanks for your reply. I have seen that. But I don' t understand how to record pure audio. please help – Chandan Reddy Sep 16 '15 at 09:44
  • 1
    AAC_ADTS is an audio format, if you don't want the MPEG4 container, but see http://stackoverflow.com/questions/21552297/how-to-record-raw-aac-audio-files-in-android-using-mediarecorder-aac-adts-doesn – headuck Sep 16 '15 at 09:56
  • Did you try using `mFileName += "/abc.mp4";` instead? – Iulian Onofrei Sep 16 '15 at 11:11
  • possible duplicate of [How to record audio file in Android](http://stackoverflow.com/questions/5696168/how-to-record-audio-file-in-android) – Karan Sharma Sep 16 '15 at 11:42
  • Possibly not, and the title may need to be changed to a less misleading one, e.g. How to set audio mime type for recorded mp4 audio. – headuck Sep 16 '15 at 13:11
  • Edited my question. please check – Chandan Reddy Sep 16 '15 at 17:02

1 Answers1

0

If all you need is to change the mime type to an audio one, you may try the following after stop() and release() of the recorder (try-catch blocks omitted for clarity):

mRecorder.stop();
mRecorder.release();
// Set the mime type to audio
String[] paths = {mFileName};
String[] mimeTypes = {"audio/mp4"};
MediaScannerConnection.scanFile(getApplicationContext(),
                                paths,
                                mimeTypes,
                                new MediaScannerConnection.OnScanCompletedListener() {
                                    @Override
                                    public void onScanCompleted(String path, Uri uri) {
                                        System.out.println("SCAN COMPLETED: " + path);

                                    }
                                });
headuck
  • 2,763
  • 16
  • 19