android - AudioRecord obtained from microphone can't be played using AudioTrack -
i use following code record data microphone , play back. i've learned buffer sizes must match. problem after record, nothing played. instead got messages on log this:
10-07 00:12:09.187: warn/audiotrack(3719): obtainbuffer() track 0x1df1a8 disabled, restarting 10-07 00:12:10.351: warn/audiotrack(3719): obtainbuffer() track 0x1df1a8 disabled, restarting
here code. do wrong?
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); log.w("debug", "audio record"); int frequency = 44100; int channelconfiguration = audioformat.channel_in_stereo; int audioencoding = audioformat.encoding_pcm_16bit; audiorecord audiorecord = null; final int buffersize = audiorecord.getminbuffersize(frequency, channelconfiguration, audioencoding); audioplayer = new audiotrack(audiomanager.stream_music, frequency, audioformat.channel_out_mono, audioformat.encoding_pcm_16bit, buffersize, audiotrack.mode_stream); log.w("debug", "buffer size: " + buffersize); //capture data , record file int readbytes = 0, writtenbytes = 0; try { audiorecord = new audiorecord(mediarecorder.audiosource.mic, frequency, channelconfiguration, audioencoding, buffersize); // audiorecord = findaudiorecord(); short[] buffer = new short[buffersize]; byte[] data = new byte[buffersize]; if (audiorecord == null || (audiorecord != null && audiorecord.getstate() != audiorecord.state_initialized)) { log.w("debug", "can't start"); //log.w("debug", "status: " + audiorecord.getstate()); return; } audiorecord.startrecording(); int samplenumber = 0; // while (samplenumber < 30) { readbytes = audiorecord.read(buffer, 0, buffersize); if (audiorecord.error_invalid_operation != readbytes) { log.w("debug", "writing"); writtenbytes += audioplayer.write(data, 0, readbytes); } log.w("debug", "sample number" + samplenumber); samplenumber++; } if (audioplayer.getplaystate() != audiotrack.playstate_playing) { log.w("debug", "playing"); audioplayer.play(); } } catch (illegalargumentexception e) { e.printstacktrace(); //to change body of catch statement use file | settings | file templates. } catch (illegalstateexception e) { e.printstacktrace(); //to change body of catch statement use file | settings | file templates. } { if (audiorecord != null && audiorecord.getstate() == audiorecord.state_initialized) { audiorecord.stop(); audiorecord.release(); } } }
the phone's volume wasn't high enough , didn't talk close enough microphone :) echo still issue, @ least can hear sounds @ last.
Comments
Post a Comment