Search code examples
androidaudiomp3

recordAudio in mp3 format using MIC in android


I want to record Audio in mp3 format using MIC in android. I try to record below code, but when i check sdcard ( path: sdcard/recording.mp3), the file does not work. (the file size is 0kb).

How can i record Mic audio in android in mp3 or wav format. please, help.

private final int FREQUENCY = 11025;
private final int CUSTOM_FREQ_SOAP = 1;
private final int OUT_FREQUENCY = FREQUENCY * CUSTOM_FREQ_SOAP;
private final int CHANNEL_CONTIGURATION = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private final int AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

======

try {
        mRecordingFile = File.createTempFile("recording", ".mp3", new File("/sdcard/"));


    } catch (IOException e) {
        throw new RuntimeException("Couldn't create file on SD card", e);
    }

======

private class RecordAudio extends AsyncTask<Void, Integer, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        isRecording = true;
        try {
            DataOutputStream dos = new DataOutputStream(
                    new BufferedOutputStream(new FileOutputStream(
                            mRecordingFile, true)));

            int bufferSize = AudioRecord.getMinBufferSize(FREQUENCY,
                    CHANNEL_CONTIGURATION, AUDIO_ENCODING);

            AudioRecord audioRecord = new AudioRecord(
                    MediaRecorder.AudioSource.MIC, FREQUENCY,
                    CHANNEL_CONTIGURATION, AUDIO_ENCODING, bufferSize);

            short[] buffer = new short[bufferSize];
            audioRecord.startRecording();

            while (isRecording) {
                int bufferReadResult = audioRecord.read(buffer, 0,
                        bufferSize);
                int amplitude = 0;
                for (int i = 0; i < bufferReadResult; i++) {
                    dos.writeShort(buffer[i]);
                    amplitude += Math.abs((int) buffer[i]);
                }
                final int amp = amplitude;

            }

            audioRecord.stop();
                //dos.close();


        } catch (Throwable t) {
        }

        return null;
    }

Solution

  • public void recordAudio(String fileName) {
        final MediaRecorder recorder = new MediaRecorder();
        ContentValues values = new ContentValues(3);
        values.put(MediaStore.MediaColumns.TITLE, fileName);
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        recorder.setOutputFile("/sdcard/sound/" + fileName);
        try {
          recorder.prepare();
        } catch (Exception e){
            e.printStackTrace();
        }
    
        final ProgressDialog mProgressDialog = new ProgressDialog(MyActivity.this);
        mProgressDialog.setTitle(R.string.lbl_recording);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setButton("Stop recording", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            mProgressDialog.dismiss();
            recorder.stop();
            recorder.release();
            }
        });
    
        mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
            public void onCancel(DialogInterface p1) {
                recorder.stop();
                recorder.release();
            }
        });
        recorder.start();
        mProgressDialog.show();
    }