I am relativity new to Android programming.
From my activity I fire off a a Asynctask that does this:
onPreExecute(): sets up the AudioRecord and gets it ready to record
doInBackground():
while (!isCancelled()) {
try
{
shortsRead = recorder.read(intputbuffer,0,size);
if (shortsRead <= 0) {
Log.e("AudioRead", "Audioread failed buffersize tried:" + " Sizeread:" + shortsRead);
recorder.stop();
recorder.release();
Thread.sleep(500);
int bufferSizeInBytes = 4096 * 2;
recorder = new AudioRecord(AudioSource.MIC, sampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes);
recorder.startRecording();
}
}
catch(Throwable x)
{
Log.w("Audio", "Error reading voice audio", x);
}
}
recorder.stop();
recorder.release();
Log.i("Audio", "Killed Audio Thread");
When I change config (switch orientation) it will run onDestroy (cancels the asynctask) and then onCreate in quick succession.
What I believe happens is the previous Asynctask is still running and not able to cancel in time and onCreate makes a new one. The new one can't get at the mic because it is already taken by my the previous asynctask.
Is there something I can do to do this cleanly?
I tried using android:configChanges="keyboardHidden|orientation"
in the AndroidManifest.xml but from what I read the problem is still there... it is just masked on an orientation change.