FileDocCategorySizeDatePackage
MicrophoneInputStream.javaAPI DocAndroid 5.1 API4556Thu Mar 12 22:22:10 GMT 2015android.speech.srec

MicrophoneInputStream

public final class MicrophoneInputStream extends InputStream
PCM input stream from the microphone, 16 bits per sample.

Fields Summary
private static final String
TAG
private long
mAudioRecord
private byte[]
mOneByte
Constructors Summary
public MicrophoneInputStream(int sampleRate, int fifoDepth)
MicrophoneInputStream constructor.

param
sampleRate sample rate of the microphone, typically 11025 or 8000.
param
fifoDepth depth of the real time fifo, measured in sampleRate clock ticks. This determines how long an application may delay before losing data.

    
                                              
           
        mAudioRecord = AudioRecordNew(sampleRate, fifoDepth);
        if (mAudioRecord == 0) throw new IOException("AudioRecord constructor failed - busy?");
        int status = AudioRecordStart(mAudioRecord);
        if (status != 0) {
            close();
            throw new IOException("AudioRecord start failed: " + status);
        }
    
Methods Summary
private static native voidAudioRecordDelete(long audioRecord)

private static native longAudioRecordNew(int sampleRate, int fifoDepth)

private static native intAudioRecordRead(long audioRecord, byte[] b, int offset, int length)

private static native intAudioRecordStart(long audioRecord)

private static native voidAudioRecordStop(long audioRecord)

public voidclose()
Closes this stream.

        if (mAudioRecord != 0) {
            try {
                AudioRecordStop(mAudioRecord);
            } finally {
                try {
                    AudioRecordDelete(mAudioRecord);
                } finally {
                    mAudioRecord = 0;
                }
            }
        }
    
protected voidfinalize()

        if (mAudioRecord != 0) {
            close();
            throw new IOException("someone forgot to close MicrophoneInputStream");
        }
    
public intread()

        if (mAudioRecord == 0) throw new IllegalStateException("not open");
        int rtn = AudioRecordRead(mAudioRecord, mOneByte, 0, 1);
        return rtn == 1 ? ((int)mOneByte[0] & 0xff) : -1;
    
public intread(byte[] b)

        if (mAudioRecord == 0) throw new IllegalStateException("not open");
        return AudioRecordRead(mAudioRecord, b, 0, b.length);
    
public intread(byte[] b, int offset, int length)

        if (mAudioRecord == 0) throw new IllegalStateException("not open");
        // TODO: should we force all reads to be a multiple of the sample size?
        return AudioRecordRead(mAudioRecord, b, offset, length);