AudioRecord 「OnRecordPositionUpdateListener」インターフェースを使ってみた。
イマイチうまく動かない。
package sakura.jp;
import java.util.Arrays;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.media.AudioRecord.OnRecordPositionUpdateListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MicTest extends Activity {
private static final int AUDIO_SAMPLE_FREQ = 8000;
private static final int AUDIO_BUFFER_SIZE = AudioRecord.getMinBufferSize(
AUDIO_SAMPLE_FREQ, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.CHANNEL_CONFIGURATION_MONO) * 2;
short[] BUFFER = new short[AUDIO_BUFFER_SIZE];
AudioRecord record;
Notification mNotification;
TextView mTextView;
Handler mHandler = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.CENTER);
setContentView(linearLayout);
record = new AudioRecord(MediaRecorder.AudioSource.MIC,
AUDIO_SAMPLE_FREQ, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, AUDIO_BUFFER_SIZE);
mNotification = new Notification();
record.setRecordPositionUpdateListener(mNotification);
record.setPositionNotificationPeriod(128);
record.setNotificationMarkerPosition(AUDIO_BUFFER_SIZE / 2);
record.startRecording();
mNotification.onPeriodicNotification(record);
mTextView = new TextView(this);
linearLayout.addView(mTextView, LayoutParams.WRAP_CONTENT);
mTextView.setText("test start");
}
public class Notification implements OnRecordPositionUpdateListener {
public void onMarkerReached(AudioRecord recorder) {
// TODO 自動生成されたメソッド・スタブ
}
public void onPeriodicNotification(AudioRecord recorder) {
// TODO 自動生成されたメソッド・スタブ
record.read(BUFFER, 0, AUDIO_BUFFER_SIZE);
Arrays.sort(BUFFER);
short volts = BUFFER[BUFFER.length - 1];
final String msg = String.valueOf(volts);
Log.d("TEST", "onPeridic : " + msg);
mHandler.post(new Runnable() {
public void run() {
mTextView.setText("onPeridic : " + msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
});
}
}
}