android - AudioRecorder can't be initialized after trying every variation -
i've read many posts problems of initialization of audiorecorder used function test every possibility. unfortunately, there no working one. should check?
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); log.w("debug", "audio record"); // capture mono data @ 16khz int frequency = 44100; int channelconfiguration = audioformat.channel_in_mono; int audioencoding = audioformat.encoding_pcm_16bit; audiorecord audiorecord=null; // minimal buffer size cannot merely 20ms of data, must // @ least 1024 bytes in case, due mmio // hardware limit. final int buffersize = audiorecord.getminbuffersize(frequency, channelconfiguration, audioencoding); log.w("debug", "buffer size: "+buffersize); try { // audiorecord = new audiorecord(mediarecorder.audiosource.mic, // frequency, channelconfiguration, // audioencoding, buffersize); audiorecord=findaudiorecord(); short[] buffer = new short[buffersize]; if(audiorecord==null || audiorecord.getstate()!= audiorecord.state_initialized) { log.w("debug", "can't start"); //log.w("debug", "status: " + audiorecord.getstate()); return; } audiorecord.startrecording(); // blocking loop uses 40% of cpu this. int samplenumber = 0; // we'll capture 3000 samples of 20ms each, // giving 60 seconds of audio data. while (samplenumber < 3000) { audiorecord.read(buffer, 0, 320); // (int = 0; < buffer.length; i++) // { // filebuffer[i * 2] = (byte) (buffer[i] & (short) 0xff); // filebuffer[i * 2 + 1] = (byte) (buffer[i] >> 8); // } samplenumber++; } } 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(); } } } private static int[] msamplerates = new int[] { 8000, 11025, 22050, 44100 }; public audiorecord findaudiorecord() { (int rate : msamplerates) { (short audioformat : new short[] { audioformat.encoding_pcm_8bit, audioformat.encoding_pcm_16bit }) { (short channelconfig : new short[] { audioformat.channel_in_mono, audioformat.channel_in_stereo }) { try { log.w("debug", "attempting rate " + rate + "hz, bits: " + audioformat + ", channel: " + channelconfig); int buffersize = audiorecord.getminbuffersize(rate, channelconfig, audioformat); if (buffersize != audiorecord.error_bad_value) { // check if can instantiate , have success audiorecord recorder = new audiorecord(mediarecorder.audiosource.default, rate, channelconfig, audioformat, buffersize); if (recorder.getstate() == audiorecord.state_initialized) return recorder; } } catch (exception e) { log.w("debug", rate + "exception, keep trying.",e); } } } } return null; }
here output:
10-06 22:18:33.828: warn/debug(26097): attempting rate 8000hz, bits: 3, channel: 16 10-06 22:18:33.828: warn/debug(26097): attempting rate 8000hz, bits: 3, channel: 12 10-06 22:18:33.828: warn/debug(26097): attempting rate 8000hz, bits: 2, channel: 16 10-06 22:18:33.867: warn/debug(26097): attempting rate 8000hz, bits: 2, channel: 12 10-06 22:18:33.875: warn/debug(26097): attempting rate 11025hz, bits: 3, channel: 16 10-06 22:18:33.875: warn/debug(26097): attempting rate 11025hz, bits: 3, channel: 12 10-06 22:18:33.875: warn/debug(26097): attempting rate 11025hz, bits: 2, channel: 16 10-06 22:18:33.906: warn/debug(26097): attempting rate 11025hz, bits: 2, channel: 12 10-06 22:18:33.929: warn/debug(26097): attempting rate 22050hz, bits: 3, channel: 16 10-06 22:18:33.929: warn/debug(26097): attempting rate 22050hz, bits: 3, channel: 12 10-06 22:18:33.929: warn/debug(26097): attempting rate 22050hz, bits: 2, channel: 16 10-06 22:18:33.929: warn/debug(26097): attempting rate 22050hz, bits: 2, channel: 12 10-06 22:18:33.929: warn/debug(26097): attempting rate 44100hz, bits: 3, channel: 16 10-06 22:18:33.929: warn/debug(26097): attempting rate 44100hz, bits: 3, channel: 12 10-06 22:18:33.929: warn/debug(26097): attempting rate 44100hz, bits: 2, channel: 16 10-06 22:18:33.937: warn/debug(26097): attempting rate 44100hz, bits: 2, channel: 12 10-06 22:18:33.937: warn/debug(26097): can't start
i have following in manifest:
uses-sdk android:minsdkversion="10" android:targetsdkversion="16" uses-permission android:name="android.permission.record_audio"
my phone galaxy ace. should check?
i restarted phone , works now. guess, calling release()
in each situation (see finally
block) essential.
Comments
Post a Comment