Example usage for android.media MediaRecorder prepare

List of usage examples for android.media MediaRecorder prepare

Introduction

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

Prototype

public void prepare() throws IllegalStateException, IOException 

Source Link

Document

Prepares the recorder to begin capturing and encoding data.

Usage

From source file:Main.java

/**
 * Then call recorder.start();//from w w  w.j  a  va 2  s .  c  om
 * @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:org.kontalk.ui.AudioFragment.java

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

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

@Override
public void run() {

    MicrophoneTaskFactory.pauseSampling();

    while (MicrophoneTaskFactory.isSampling()) {
        try {//from  w w w.  j  a  va2s .  co 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 w  w. j av a2 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:com.codename1.impl.android.AndroidImplementation.java

@Override
public Media createMediaRecorder(final String path, final String mimeType) throws IOException {
    if (getActivity() == null) {
        return null;
    }/* w  ww  . j  a v  a  2 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];
    }
}