简体   繁体   中英

How to Record voice with Audio MimeType in Android

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.

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);

                                    }
                                });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM