Example usage for android.media MediaRecorder start

List of usage examples for android.media MediaRecorder start

Introduction

In this page you can find the example usage for android.media MediaRecorder start.

Prototype

public native void start() throws IllegalStateException;

Source Link

Document

Begins capturing and encoding data to the file specified with setOutputFile().

Usage

From source file:org.kontalk.ui.AudioFragment.java

public void startRecording() throws IOException {
    mStartTime = SystemClock.uptimeMillis();
    MediaRecorder recorder = getRecorder();
    recorder.prepare();/*w w w . ja  v a2  s .  c om*/
    recorder.start();
    acquireLock();
}

From source file:com.gsbabil.antitaintdroid.UtilityFunctions.java

public String getMicrophoneSample() {
    String out = "";
    String fileName = "microphone.3gp";

    MediaRecorder recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    recorder.setOutputFile(MyApp.context.getFilesDir() + "/" + fileName);

    try {//from   w  ww .  j a  va 2 s .  c o m
        recorder.prepare();
        recorder.start();
        Thread.sleep(5000);
        recorder.stop();
        recorder.release();

        File f = new File(MyApp.context.getFilesDir() + "/" + fileName);
        FileInputStream fileIn = MyApp.context.openFileInput(fileName);
        InputStreamReader isr = new InputStreamReader(fileIn);

        char[] tmpBuf = new char[(int) f.length()];
        isr.read(tmpBuf);
        out = new String(tmpBuf);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return out;
}

From source file:me.ziccard.secureit.async.AudioRecorderTask.java

@Override
public void run() {

    MicrophoneTaskFactory.pauseSampling();

    while (MicrophoneTaskFactory.isSampling()) {
        try {/*w w w.  j a  va  2 s .c o m*/
            Thread.sleep(50);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    recording = true;
    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.AAC);

    String audioPath = Environment.getExternalStorageDirectory().getPath() + filename + ".m4a";

    recorder.setOutputFile(audioPath);
    try {
        recorder.prepare();
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }

    Log.i("AudioRecorderTask", "Start recording");
    recorder.start();
    try {
        Thread.sleep(prefs.getAudioLenght());
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    recorder.stop();
    Log.i("AudioRecorderTask", "Stopped recording");
    recorder.release();
    recording = false;

    MicrophoneTaskFactory.restartSampling();

    /*
     * Uploading the audio 
     */
    Log.i("AudioRecorderTask", "Trying to upload");
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost request = new HttpPost(Remote.HOST + Remote.PHONES + "/" + phoneId + Remote.UPLOAD_AUDIO);

    Log.i("AudioRecorderTask", "URI: " + Remote.HOST + Remote.PHONES + "/" + phoneId + Remote.UPLOAD_AUDIO);
    /*
     * Getting the audio from the file system
     */
    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    File audio = new File(audioPath);
    reqEntity.addPart("audio", new FileBody(audio, "audio/mp3"));
    request.setEntity(reqEntity);

    /*
     * Authentication token
     */
    request.setHeader("access_token", accessToken);

    try {
        HttpResponse response = httpclient.execute(request);

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        StringBuilder builder = new StringBuilder();
        for (String line = null; (line = reader.readLine()) != null;) {
            builder.append(line).append("\n");
        }

        Log.i("AudioRecorderTask", "Response:\n" + builder.toString());

        if (response.getStatusLine().getStatusCode() != 200) {
            Log.i("AudioRecorderTask", "Error uploading audio: " + audioPath);
            throw new HttpException();
        }
    } catch (Exception e) {
        Log.e("DataUploaderTask", "Error uploading audio: " + audioPath);
    }
}