FileDocCategorySizeDatePackage
Recorder.javaAPI DocAndroid 1.5 API6907Wed May 06 22:42:48 BST 2009com.android.soundrecorder

Recorder

public class Recorder extends Object implements android.media.MediaPlayer.OnErrorListener, android.media.MediaPlayer.OnCompletionListener

Fields Summary
static final String
SAMPLE_PREFIX
static final String
SAMPLE_PATH_KEY
static final String
SAMPLE_LENGTH_KEY
public static final int
IDLE_STATE
public static final int
RECORDING_STATE
public static final int
PLAYING_STATE
int
mState
public static final int
NO_ERROR
public static final int
SDCARD_ACCESS_ERROR
public static final int
INTERNAL_ERROR
OnStateChangedListener
mOnStateChangedListener
long
mSampleStart
int
mSampleLength
File
mSampleFile
android.media.MediaRecorder
mRecorder
android.media.MediaPlayer
mPlayer
Constructors Summary
public Recorder()

    
      
    
Methods Summary
public voidclear()
Resets the recorder state. If a sample was recorded, the file is left on disk and will be reused for a new recording.

        stop();
        
        mSampleLength = 0;
        
        signalStateChanged(IDLE_STATE);
    
public voiddelete()
Resets the recorder state. If a sample was recorded, the file is deleted.

        stop();
        
        if (mSampleFile != null)
            mSampleFile.delete();

        mSampleFile = null;
        mSampleLength = 0;
        
        signalStateChanged(IDLE_STATE);
    
public intgetMaxAmplitude()

        if (mState != RECORDING_STATE)
            return 0;
        return mRecorder.getMaxAmplitude();
    
public voidonCompletion(android.media.MediaPlayer mp)

        stop();
    
public booleanonError(android.media.MediaPlayer mp, int what, int extra)

        stop();
        setError(SDCARD_ACCESS_ERROR);
        return true;
    
public intprogress()

        if (mState == RECORDING_STATE || mState == PLAYING_STATE)
            return (int) ((System.currentTimeMillis() - mSampleStart)/1000);
        return 0;
    
public voidrestoreState(android.os.Bundle recorderState)

        String samplePath = recorderState.getString(SAMPLE_PATH_KEY);
        if (samplePath == null)
            return;
        int sampleLength = recorderState.getInt(SAMPLE_LENGTH_KEY, -1);
        if (sampleLength == -1)
            return;

        File file = new File(samplePath);
        if (!file.exists())
            return;
        if (mSampleFile != null
                && mSampleFile.getAbsolutePath().compareTo(file.getAbsolutePath()) == 0)
            return;
        
        delete();
        mSampleFile = file;
        mSampleLength = sampleLength;

        signalStateChanged(IDLE_STATE);
    
public java.io.FilesampleFile()

        return mSampleFile;
    
public intsampleLength()

        return mSampleLength;
    
public voidsaveState(android.os.Bundle recorderState)

        recorderState.putString(SAMPLE_PATH_KEY, mSampleFile.getAbsolutePath());
        recorderState.putInt(SAMPLE_LENGTH_KEY, mSampleLength);
    
private voidsetError(int error)

        if (mOnStateChangedListener != null)
            mOnStateChangedListener.onError(error);
    
public voidsetOnStateChangedListener(com.android.soundrecorder.Recorder$OnStateChangedListener listener)

        mOnStateChangedListener = listener;
    
private voidsetState(int state)

        if (state == mState)
            return;
        
        mState = state;
        signalStateChanged(mState);
    
private voidsignalStateChanged(int state)

        if (mOnStateChangedListener != null)
            mOnStateChangedListener.onStateChanged(state);
    
public voidstartPlayback()

        stop();
        
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mSampleFile.getAbsolutePath());
            mPlayer.setOnCompletionListener(this);
            mPlayer.setOnErrorListener(this);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IllegalArgumentException e) {
            setError(INTERNAL_ERROR);
            mPlayer = null;
            return;
        } catch (IOException e) {
            setError(SDCARD_ACCESS_ERROR);
            mPlayer = null;
            return;
        }
        
        mSampleStart = System.currentTimeMillis();
        setState(PLAYING_STATE);
    
public voidstartRecording(int outputfileformat, java.lang.String extension)

        stop();
        
        if (mSampleFile == null) {
            File sampleDir = Environment.getExternalStorageDirectory();
            if (!sampleDir.canWrite()) // Workaround for broken sdcard support on the device.
                sampleDir = new File("/sdcard/sdcard");
            
            try {
                mSampleFile = File.createTempFile(SAMPLE_PREFIX, extension, sampleDir);
            } catch (IOException e) {
                setError(SDCARD_ACCESS_ERROR);
                return;
            }
        }
        
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(outputfileformat);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile(mSampleFile.getAbsolutePath());

        // Handle IOException
        try {
            mRecorder.prepare();
        } catch(IOException exception) {
            setError(INTERNAL_ERROR);
            mRecorder.reset();
            mRecorder.release();
            mRecorder = null;
            return;
        }
        mRecorder.start();

        mSampleStart = System.currentTimeMillis();
        setState(RECORDING_STATE);
    
public intstate()

        return mState;
    
public voidstop()

        stopRecording();
        stopPlayback();
    
public voidstopPlayback()

        if (mPlayer == null) // we were not in playback
            return;

        mPlayer.stop();
        mPlayer.release();
        mPlayer = null;
        setState(IDLE_STATE);
    
public voidstopRecording()

        if (mRecorder == null)
            return;

        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;

        mSampleLength = (int)( (System.currentTimeMillis() - mSampleStart)/1000 );
        setState(IDLE_STATE);