I'm using AudioRecord in a separate class, and I have it read a buffer
in a loop and callback to the main activity with the buffer.
The activity processes the buffer and calculates a number that I want
to update the UI with (I'm using animation to move a button). What
I'm finding is that first all the buffers are getting processed, and
then after that's done, the handler is apparently handling all the
messages and doing all the UI updates one after the other.
Perhaps this is working as designed because I think the handler
message processing occurs after the other work is done. If this is
true, what can I do to process a buffer and then immediately update
the UI (and do that for each buffer)?
Here's a snippet of the recording part. In the constructor, I'm
passing in a reference to the activity so I can callback to it.
Recorder(myActivity act)
{
super();
this.setFrequency(frequency);
this.setChannelConfiguration(AudioFormat.CHANNEL_IN_MONO);
this.setPaused(false);
this.act = act;
}
AudioRecord recordInstance = new AudioRecord(
MediaRecorder.AudioSource.MIC,
this.getFrequency(),
this.getChannelConfiguration(),
this.getAudioEncoding(),
bufferSize);
recordInstance.startRecording();
buffer = new byte[bufferSize];
while (this.isRecording) {
// Are we paused?
synchronized(mutex)
{
if (this.isPaused)
{
try
{
mutex.wait(250);
}
catch
(InterruptedException e)
{
throw new
IllegalStateException("Wait() interrupted!",e);
}
continue;
}
}
bufferRead =
recordInstance.read(buffer, 0, bufferSize);
if (bufferRead ==
AudioRecord.ERROR_INVALID_OPERATION)
{
throw new
IllegalStateException("read() returned
AudioRecord.ERROR_INVALID_OPERATION");
}
else if (bufferRead ==
AudioRecord.ERROR_BAD_VALUE)
{
throw new
IllegalStateException("read() returned
AudioRecord.ERROR_BAD_VALUE");
}
else if (bufferRead ==
AudioRecord.ERROR_INVALID_OPERATION)
{
throw new
IllegalStateException("read() returned
AudioRecord.ERROR_INVALID_OPERATION");
}
// Write buffer to file
try
{
fWriter.write(buffer);
}
catch (Exception e)
{
Log.d(Recorder.class.getName(),
e.getMessage());
}
payloadSize += buffer.length;
// Tell activity we have a buffer to
process
act.startLongRunningOperation();
}
Here's what I have in the activity (I instantiate Recorder in
onCreate()):
// Need handler for callbacks to the UI thread
final Handler mHandler = new Handler();
// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
updateResultsInUi();
}
};
protected void startLongRunningOperation()
{
// Fire off a thread to do some work that we shouldn't do
directly in the UI thread
Thread t = new Thread() {
public void run() {
mHandler.postAtFrontOfQueue(mUpdateResults);
}
};
t.start();
}
private void updateResultsInUi()
{
// Back in the UI thread -- update our UI elements based on the
data in mResults
animateButton();
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en