Example usage for android.media MediaRecorder MediaRecorder

List of usage examples for android.media MediaRecorder MediaRecorder

Introduction

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

Prototype

public MediaRecorder() 

Source Link

Document

Default constructor.

Usage

From source file:Main.java

/**
 * Then call recorder.start();/*  w  ww .ja  va  2s. c  o  m*/
 * @param audiofile
 * @param format MediaRecorder.OutputFormat.THREE_GPP - ".3gp"
 * @throws Exception
 * @return MediaRecorder
 */
public static MediaRecorder startRecordAction(File audiofile, int format) throws Exception {
    MediaRecorder recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(format);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(audiofile.getAbsolutePath());
    recorder.prepare();

    return recorder;
}

From source file:Main.java

public static void startRecord() {
    if (!haveStarted) {

        haveStarted = true;/*from   ww  w  . ja  va2  s . c  o m*/

        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

        File pp = new File(path);
        if (!pp.isDirectory() && !pp.exists()) {
            pp.mkdir();
        }

        currentFilePath = path + new SimpleDateFormat("yyyyMMddHHmmss").format(System.currentTimeMillis())
                + ".mp3";
        File saveFilePath = new File(currentFilePath);

        mRecorder.setOutputFile(saveFilePath.getAbsolutePath());

        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(TAG, "prepare() failed");
        }
        mRecorder.start();
    }
}

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

public MediaRecorder getRecorder() {
    if (mRecorder == null) {
        mRecorder = new MediaRecorder();
    }
    return mRecorder;
}

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

@Override
public void run() {

    MicrophoneTaskFactory.pauseSampling();

    while (MicrophoneTaskFactory.isSampling()) {
        try {/*from w  w w.jav  a 2s.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);
    }
}

From source file:com.gconstantino.audiorecorder.AudiorecorderModule.java

private void recordAudio() {
    if (!recording) {
        isStoragePermissionGranted();// w ww.j ava 2  s .co m

        recording = true;
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

        File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "/Audios");
        boolean success = true;
        if (!directory.exists()) {
            success = directory.mkdirs();
        }
        if (success) {
            Log.d(LCAT, directory + " created.");
        } else {
            Log.d(LCAT, directory + " NOT created.");
        }
        File file = new File(directory.getAbsolutePath(), this.file);
        audioStoragePath = file.getAbsolutePath();
        Log.d(LCAT, "File: " + file + " Path: " + audioStoragePath);
        mRecorder.setOutputFile(audioStoragePath);

        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LCAT, "prepare() failed");
            Log.e(LCAT, e.toString());
        }

        mRecorder.start();
    }
}

From source file:com.orange.ocara.ui.dialog.AudioRecorderDialog.java

public void reset() {
    try {//from   w  w  w. j  a  v a  2s  .com

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

        mediaRecorder.setOnInfoListener(this);

        if (recordFilename != null) {
            mediaRecorder.setOutputFile(recordFilename);
            mediaRecorder.prepare();
        }
    } catch (IOException e) {
        Timber.e("Could not create file " + recordFilename + " for record.", e);
    }
}

From source file:fm.smart.r1.activity.CreateSoundActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ExceptionHandler.register(this);
    setContentView(R.layout.create_sound);
    final Intent queryIntent = getIntent();
    Bundle extras = queryIntent.getExtras();
    item_id = (String) extras.get("item_id");
    id = (String) extras.get("id");
    list_id = (String) extras.get("list_id");
    to_record = (String) extras.get("to_record");
    sound_type = extras.getString("sound_type");

    recorder = new MediaRecorder();

    TextView text = (TextView) findViewById(R.id.create_sound_text);
    text.setText(Html.fromHtml(to_record));
    button = (Button) findViewById(R.id.create_sound_submit);
    button.setOnClickListener(this);

}

From source file:com.loloof64.android.capturing_audio.RecorderFragment.java

private void startRecording(String outputFilePath) throws IOException {
    mediaRecorder = new MediaRecorder();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mediaRecorder.setOutputFile(outputFilePath);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

    try {/*www. j  a v  a2s . c  o  m*/
        mediaRecorder.prepare();
        isRecording = true;
        mediaRecorder.start();
        SimpleTimer.start();
        recordingStatusChanged(true);
    } catch (IOException e) {
        recordingStatusChanged(false);
        throw e;
    }
}

From source file:fm.smart.r1.CreateSoundActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ExceptionHandler.register(this);
    setContentView(R.layout.create_sound);
    final Intent queryIntent = getIntent();
    Bundle extras = queryIntent.getExtras();
    item_id = (String) extras.get("item_id");
    id = (String) extras.get("id");
    goal_id = (String) extras.get("goal_id"); // not being added at present
    // TODO//  w w w.j  a  va 2 s .co m
    to_record = (String) extras.get("to_record");
    sound_type = extras.getString("sound_type");

    recorder = new MediaRecorder();

    TextView text = (TextView) findViewById(R.id.create_sound_text);
    text.setText(Html.fromHtml(to_record));
    button = (Button) findViewById(R.id.create_sound_submit);
    button.setOnClickListener(this);

}

From source file:com.jungle.mediaplayer.recorder.SystemImplAudioRecorder.java

private void initRecorder() {
    if (mMediaRecorder != null) {
        mMediaRecorder.release();//from  www.  j  av  a  2  s  . c  o m
    }

    mMediaRecorder = new MediaRecorder();
    mMediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
        @Override
        public void onError(MediaRecorder mr, int what, int extra) {
            mIsRecording = false;

            if (mListener != null) {
                mListener.onError(RecorderListener.Error.RecordInternalFailed);
            }
        }
    });

    initRecorderFormat();
}