Example usage for android.media MediaRecorder setAudioEncoder

List of usage examples for android.media MediaRecorder setAudioEncoder

Introduction

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

Prototype

public native void setAudioEncoder(int audio_encoder) throws IllegalStateException;

Source Link

Document

Sets the audio encoder to be used for recording.

Usage

From source file:Main.java

/**
 * Then call recorder.start();/*from w w  w .ja v a 2 s.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:me.ziccard.secureit.async.AudioRecorderTask.java

@Override
public void run() {

    MicrophoneTaskFactory.pauseSampling();

    while (MicrophoneTaskFactory.isSampling()) {
        try {/* w  w w .  j a va2 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);
    }
}

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 .jav  a 2s .  co  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:org.kontalk.ui.AudioDialog.java

/**
 * Begins recording audio./*from ww  w.  j a  v  a 2 s . c  o m*/
 * @throws IOException if writing to storage failed
 */
void startRecord() throws IOException {
    mFile = MediaStorage.getOutgoingAudioFile();
    setupViewForRecording(0);

    try {
        MediaRecorder recorder = mData.getRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setOutputFile(mFile.getAbsolutePath());
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        // start recording
        mData.startRecording();
        mStatus = STATUS_RECORDING;
    } catch (IllegalStateException e) {
        Log.e(TAG, "error starting audio recording", e);
    } catch (IOException e) {
        Log.e(TAG, "error writing on external storage", e);
        cancel();
        new MaterialDialog.Builder(getContext()).content(R.string.err_audio_record_writing)
                .positiveText(android.R.string.ok).show();
    } catch (RuntimeException e) {
        Log.e(TAG, "error starting audio recording", e);
        cancel();
        new MaterialDialog.Builder(getContext()).content(R.string.err_audio_record)
                .positiveText(android.R.string.ok).show();
    }
}

From source file:com.codename1.impl.android.AndroidImplementation.java

@Override
public Media createMediaRecorder(final String path, final String mimeType) throws IOException {
    if (getActivity() == null) {
        return null;
    }//from w  ww  . ja  va2 s .  c o m
    if (!checkForPermission(Manifest.permission.RECORD_AUDIO, "This is required to record audio")) {
        return null;
    }
    final AndroidRecorder[] record = new AndroidRecorder[1];
    final IOException[] error = new IOException[1];

    final Object lock = new Object();
    synchronized (lock) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock) {
                    MediaRecorder recorder = new MediaRecorder();
                    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    if (mimeType.contains("amr")) {
                        recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
                        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    } else {
                        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
                    }
                    recorder.setOutputFile(removeFilePrefix(path));
                    try {
                        recorder.prepare();
                        record[0] = new AndroidRecorder(recorder);
                    } catch (IllegalStateException ex) {
                        Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        error[0] = ex;
                    } finally {
                        lock.notify();
                    }

                }
            }
        });

        try {
            lock.wait();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }

        if (error[0] != null) {
            throw error[0];
        }

        return record[0];
    }
}