Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Record Voice in android?

I am trying to record the voice in android But it will create the .mp3 file on the path (sdcard/filename) But when i run this file it doesen't play because it doesn't record the voice.

Here is My code

public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case(R.id.Button01):
            try {
                //audio.start();
                startRecord();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        case(R.id.Button02):
            //audio.stop();
            stopRecord();
        }

    }
     private void startRecord() throws IllegalStateException, IOException{
           // recorder = new MediaRecorder(); 
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);  //ok so I say audio source is the microphone, is it windows/linux microphone on the emulator? 
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
            recorder.setOutputFile("/sdcard/Music/"+System.currentTimeMillis()+".amr"); 
            recorder.prepare(); 
            recorder.start();      
        }

        private void stopRecord(){
            recorder.stop();
          //recorder.release();
        }



}

Manifest file

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
like image 222
Amandeep singh Avatar asked Nov 25 '11 03:11

Amandeep singh


2 Answers

Refer to the Android Audio Capture documentation for recording audio and playing back the recorded audio.

like image 189
ilango j Avatar answered Oct 30 '22 21:10

ilango j


import java.io.File;
import java.io.IOException;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;

public class AudioRecorder {

    final MediaRecorder recorder = new MediaRecorder();
    public final String path;

    public AudioRecorder(String path) {
        this.path = sanitizePath(path);
    }

    private String sanitizePath(String path) {
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        if (!path.contains(".")) {
            path += ".3gp";
        }
        return Environment.getExternalStorageDirectory().getAbsolutePath()
                + path;
    }

    public void start() throws IOException {
        String state = android.os.Environment.getExternalStorageState();
        if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
            throw new IOException("SD Card is not mounted.  It is " + state
                    + ".");
        }

        // make sure the directory we plan to store the recording in exists
        File directory = new File(path).getParentFile();
        if (!directory.exists() && !directory.mkdirs()) {
            throw new IOException("Path to file could not be created.");
        }

        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(path);
        recorder.prepare();
        recorder.start();
    }

    public void stop() throws IOException {
        recorder.stop();
        recorder.release();
    }

    public void playarcoding(String path) throws IOException {
        MediaPlayer mp = new MediaPlayer();
        mp.setDataSource(path);
        mp.prepare();
        mp.start();
        mp.setVolume(10, 10);
    }
}
like image 8
ud_an Avatar answered Oct 30 '22 20:10

ud_an