How to Record voice with Audio MimeType in Android

泄露秘密 提交于 2019-12-08 11:09:04

问题


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.


回答1:


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

                                    }
                                });


来源:https://stackoverflow.com/questions/32604785/how-to-record-voice-with-audio-mimetype-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!