Recording Audio : Audio « Media « Android






Recording Audio

   
//package com.zyc.misc;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaRecorder;

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

/**
 * @author ZengYongchang@gmail.com
 */
class AudioUtil {
    private MediaRecorder mMediaRecorder;


    public void startRecordingAudio(Context context, String audioFileName) {
        try {
            mMediaRecorder = new MediaRecorder();
            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            mMediaRecorder.setOutputFile(context.openFileOutput(audioFileName, Context.MODE_PRIVATE).getFD());
            mMediaRecorder.prepare();
            mMediaRecorder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void stopRecordingAudio() {
        mMediaRecorder.stop();
        mMediaRecorder.release();
        mMediaRecorder = null;
    }


    public void playAudio(String audioFilePath) {
        try {
            MediaPlayer mp = new MediaPlayer();
            mp.reset();
            mp.setDataSource((new FileInputStream(new File(audioFilePath))).getFD());
            mp.prepare();
            mp.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

   
    
    
  








Related examples in the same category

1.Record audio to 3gpp file
2.Audio Recording
3.Audio recoding and call back
4.Using BackgroundAudioServiceBinder
5.Custom Audio Recorder
6.Set Audio source and format
7.Audio processing library
8.Audio Synthesis
9.Audio Synthesis 440 Hz
10.Audio Files Filter
11.AudioClip
12.Random Media Player