Code example for AudioRecord

Methods: getStatereadreleasestartRecordingstop

0
        int sampleRateInHz = 8000;
        int channelConfig = android.media.AudioFormat.CHANNEL_IN_DEFAULT;
        int audioFormat = android.media.AudioFormat.ENCODING_DEFAULT;
        int minBufSize = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
    	try{ 
    		mRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, channelConfig, audioFormat, sizeInBytes);
    	} 
    	catch(IllegalArgumentException e){
    		alertBox("new AudioRecord failed", e.toString());
    	} 
    	alertBox("state", mRecorder.getState());
    	 
    	if( mRecorder.getState() == AudioRecord.STATE_INITIALIZED)
    	{ 
	    	try{ 
	    		mRecorder.startRecording();
	    	}catch( IllegalStateException e)
	    	{ 
	    		alertBox(".startRecording() fail:", e.toString());
	    	} 
	    	result = mRecorder.read(audioData, offsetInBytes, sizeInBytes);
			if( result == AudioRecord.ERROR_BAD_VALUE || result == AudioRecord.ERROR_INVALID_OPERATION)
				alertBox(".read() failed", result);
			 
	        alertBox("mRecorder.read:", audioData.toString());
	         
	        mRecorder.stop();
	        mRecorder.release();
    	} 
    	else if ( mRecorder.getState() == AudioRecord.STATE_UNINITIALIZED )
    		alertBox("Uninitialized", "Uninitialized state for AudioRecord object"); 
    		 
    }