Android AudioRecord - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android AudioRecord class.

This code example shows how to use the following methods:release, stop
	@Override 
	public void appStop(){ 
		threadRunning = false;
		 
		if(iTrack!=null){
			iTrack.stop();
		} 
		 
		try { 
			thread.join();
		} catch (InterruptedException e) {
			Log.e("OF", "error finishing audio thread ", e);
		} 
		thread = null;
		 
 
		if(oTrack!=null){
			oTrack.stop();
			oTrack.release();
			oTrack = null;

8
CodeRank
This code example shows how to use the following methods:getAudioFormat, getChannelCount, getSampleRate, release
										AudioFormat.ENCODING_PCM_8BIT;
 
		int minBufDevice = AudioRecord.getMinBufferSize(rate, channelConfig, encodingConfig);
		int minBufferSize = Math.max(bufsize * 8, minBufDevice + (bufsize - (minBufDevice % bufsize)));
		Log.i("SDL", "SDL: app opened recording device, rate " + rate + " channels " + channels + " sample size " + (encoding+1) + " bufsize " + bufsize + " internal bufsize " + minBufferSize);
		if( mRecorder == null || mRecorder.getSampleRate() != rate ||
			mRecorder.getChannelCount() != channels ||
			mRecorder.getAudioFormat() != encodingConfig ||
			mRecorderBufferSize != minBufferSize )
		{ 
			if( mRecorder != null )
				mRecorder.release();
			mRecorder = null;
			try { 
				mRecorder = new AudioRecord(AudioSource.DEFAULT, rate, channelConfig, encodingConfig, minBufferSize);
				mRecorderBufferSize = minBufferSize;
			} catch (IllegalArgumentException e) {
				Log.i("SDL", "SDL: error: failed to open recording device!");
				return null; 
			} 
7
CodeRank
This code example shows how to use the following methods:getState, startRecording
 
		// ensure minimum record buffer 
		if (bufferSize < minimumBufferSize)
			bufferSize = minimumBufferSize;
 
		AudioRecord recorder = new AudioRecord(audioSource,
				sampleRateInHz, channelConfig, audioFormat, bufferSize);
 
		if (recorder.getState() != AudioRecord.STATE_INITIALIZED) {
			recorder.release();
			throw new IOException("Audio preparation failed");
		} 
 
		recorder.startRecording();
		this.audioRecorder = recorder;
		this.echoCanceller = echoCanceller;
	} 
 
	@Override 
	public void close() throws IOException { 
7
CodeRank
This code example shows how to use the following methods:getState, setPositionNotificationPeriod, setRecordPositionUpdateListener
				// Set frame period and timer interval accordingly 
				mPeriodInFrames = mBufferSize / ( 2 * mBitsPersample * mNumOfChannels / 8 );
				Log.w(PcmAudioRecorder.class.getName(), "Increasing buffer size to " + Integer.toString(mBufferSize));
			} 
			 
			audioRecorder = new AudioRecord(audioSource, sampleRate, channelConfig, audioFormat, mBufferSize);
			if (audioRecorder.getState() != AudioRecord.STATE_INITIALIZED) {
				throw new Exception("AudioRecord initialization failed");
			} 
			audioRecorder.setRecordPositionUpdateListener(updateListener);
			audioRecorder.setPositionNotificationPeriod(mPeriodInFrames);
			filePath = null;
			state = State.INITIALIZING;
		} catch (Exception e) {
			if (e.getMessage() != null) {
				Log.e(PcmAudioRecorder.class.getName(), e.getMessage());
			} else { 
				Log.e(PcmAudioRecorder.class.getName(), "Unknown error occured while initializing recording");
			} 
			state = State.ERROR;
7
CodeRank
This code example shows how to use the following methods:getRecordingState, getState, startRecording
    protected int getOutboundTimestamp() { 
        return (int) _countFrames * _audio.getFrameInterval();
    } 
 
    protected void startRec() { 
        if (_mic != null && _mic.getState() == AudioRecord.STATE_INITIALIZED) {
            Log.debug( 
                    this.getClass().getSimpleName()
                    + ".startRec(): starting mic"); 
            _mic.startRecording();
            resetTimestampRecStart(); 
            _countFrames = 0;
 
            Log.debug( 
                    this.getClass().getSimpleName() + ".startRec(): state="
                    + _mic.getState() + ", recordingState="
                    + _mic.getRecordingState());
 
            if (_me == null) {
                _me = new Thread(this, "mic");

8
CodeRank
This code example shows how to use the following methods:getRecordingState, release, stop
            } 
        } 
 
        public void cleanup() { 
            // stop things 
            if (audioRecord.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) {
                audioRecord.stop();
            } 
            audioRecord.release();
            if (isLiveMode) {
                audioTrack.stop();
                audioTrack.release();
            } 
            if (instrumentalReader != null) {
                try { 
                    Resample.close(); 
                    instrumentalReader.closeWaveFile();
                } catch (IOException e) {
                    // no recovery possible here 
                    e.printStackTrace();
7
CodeRank
This code example shows how to use the following methods:getRecordingState, getState, read, startRecording, stop
		return tmpData;
	} 
 
	  private byte[] fillBuffer(byte[] audioData, int bufferSize) {
 
		    AudioRecord recorder = new AudioRecord(AudioSource.MIC, 8000, AudioFormat.CHANNEL_IN_MONO,
		            AudioFormat.ENCODING_PCM_16BIT, bufferSize); // instantiate the
		                                                            // AudioRecorder 
		    if (recorder.getRecordingState() == android.media.AudioRecord.RECORDSTATE_STOPPED)
		        recorder.startRecording(); // check to see if the Recorder
		                                    // has stopped or is not 
		                                    // recording, and make it 
		                                    // record. 
 
		    recorder.read(audioData, 0, bufferSize); // read the PCM
		                                                // audio data 
		                                                // into the 
		                                                // audioData 
		                                                // array 
 

6
CodeRank
This code example shows how to use the following methods:getAudioFormat, getChannelCount, getSampleRate, release
            AudioFormat.ENCODING_PCM_8BIT;
 
        int minBufDevice = AudioRecord.getMinBufferSize(rate, channelConfig, encodingConfig);
        int minBufferSize = Math.max(bufsize * 8, minBufDevice + (bufsize - (minBufDevice % bufsize)));
        System.out.println("SDL: app opened recording device, rate " + rate + " channels " + channels + " sample size " + (encoding+1) + " bufsize " + bufsize + " internal bufsize " + minBufferSize);
        if( mRecorder == null || mRecorder.getSampleRate() != rate ||
            mRecorder.getChannelCount() != channels ||
            mRecorder.getAudioFormat() != encodingConfig ||
            mRecorderBufferSize != minBufferSize )
        { 
            if( mRecorder != null )
                mRecorder.release();
            mRecorder = null;
            try { 
                mRecorder = new AudioRecord(AudioSource.DEFAULT, rate, channelConfig, encodingConfig, minBufferSize);
                mRecorderBufferSize = minBufferSize;
            } catch (IllegalArgumentException e) {
                System.out.println("SDL: error: failed to open recording device!");
                return null; 
            } 
6
CodeRank
This code example shows how to use the following methods:startRecording
 
	public AndroidAudioRecorder (int samplingRate, boolean isMono) {
		int minBufferSize = AudioRecord.getMinBufferSize(samplingRate, isMono ? AudioFormat.CHANNEL_CONFIGURATION_MONO
			: AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT);
		recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, samplingRate, isMono ? AudioFormat.CHANNEL_CONFIGURATION_MONO
			: AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize);
		recorder.startRecording();
	} 
 
	@Override public void dispose () { 
		recorder.stop();
		recorder.release();
	} 
 
	@Override public void read (short[] samples, int offset, int numSamples) {
		int read = 0;
		while (read != numSamples) {
			read += recorder.read(samples, offset + read, numSamples - read);
		} 
	} 
6
CodeRank
This code example shows how to use the following methods:read, release, startRecording, stop
    boolean stopC;
    public int minBuffer = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
    MediaRecorder recorder = new MediaRecorder();
 
    public short[] startCapture() { 
        AudioRecord record = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, 44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, (minBuffer * 100));
        short[] point = new short[minBuffer * 100];
        record.startRecording();
        System.out.println(minBuffer * 100);
        nbPoint = record.read(point, 0, point.length);
        System.out.println("fin record");
        record.stop();
        record.release();
        return point;
    } 
 
    public void writeSound(short[] point) {
        AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, (minBuffer * 200), AudioTrack.MODE_STATIC);
        track.write(point, 0, point.length);
        track.play();
5
CodeRank

Related Android AudioRecord Questions & Answers:

See Code Examples for Android AudioRecord Methods: