Hi everyone,
I'm a student from Spain who is developing a project with Android
where I try to connect Android devices with Arduino microcontroller. I
explain everything here in my blog, wehre I comment every progress I
make.
At this moment I'm trying to record some sound through the device's
MIC and try to recover it from an array where I save all the data.
Here is the code:
____________________________________________________________________________________
package org.ardroino.prototype;
import android.app.Activity;
import android.os.Bundle;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Context;
//import java.nio.ByteBuffer;
//import android.media.AudioTrack;
//import android.media.MediaRecorder.AudioSource;
public class HelloworldActivity extends Activity {
/** Called when the activity is first created. */
private static final int AUDIO_SAMPLE_FREQ = 8000;
// private static final int AUDIO_BUFFER_SIZE=11025;
private TextView TextAudio;
private String msg2 = "";
private String msg1 = "";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setButtonClickListener();
}
private void setButtonClickListener() {
Button ButtonRec = (Button) findViewById(R.id.Rec);
TextAudio = (TextView) findViewById(R.id.Values);
ButtonRec.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
recordAudio();
} catch (Exception e) {
e.printStackTrace();
debugMessage("Error" + e.getMessage());
}
}
});
}
protected void onResume() {
super.onResume();
}
private void recordAudio() {
debugMessage("recordAudio():");
int AUDIO_BUFFER_SIZE =
AudioTrack.getMinBufferSize(AUDIO_SAMPLE_FREQ,
2,
AudioFormat.ENCODING_PCM_16BIT);
appendDebugInfo("BufferSize", ""+ AUDIO_BUFFER_SIZE );
byte[] audioData = new byte[AUDIO_BUFFER_SIZE];
AudioRecord localAudioRecord = new AudioRecord(
MediaRecorder.AudioSource.MIC,
AUDIO_SAMPLE_FREQ,
2,
AudioFormat.ENCODING_PCM_16BIT,
AUDIO_BUFFER_SIZE);
AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC,
AUDIO_SAMPLE_FREQ, 2,
AudioFormat.ENCODING_PCM_16BIT,
AUDIO_BUFFER_SIZE,
AudioTrack.MODE_STREAM);
// audio recording
localAudioRecord.startRecording();
int nBytes=localAudioRecord.read(audioData, 0,
AUDIO_BUFFER_SIZE);
//atrack.write(buffer, 0, buffer.length);
appendDebugInfo("NBytes", ""+ nBytes );
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
// stop and free resources
//track.write(audioData, 0, AUDIO_BUFFER_SIZE);
localAudioRecord.stop();
localAudioRecord.release();
// process audio data
showAudioData(audioData, nBytes);
}
private void appendDebugInfo(String label, String msg){
String txt = "" + TextAudio.getText();
TextAudio.setText(label + ":" + msg + "\n" + txt);
}
// print data
private void showAudioData(byte[] audioData, int nBytes) {
debugMessage("showAudioData():");
String msg = ""+ TextAudio.getText()+"\n";
for (int i = 1; i < (audioData.length)/2; i++) {
//msg += audioData[2*(i-1)] + "\n";
msg += audioData[i] + "\n";
}
TextAudio.setText(msg);
}
private void debugMessage(String msg) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, msg, duration);
toast.show();
}
__________________________________________________________________________________
My problem is that I don't know why but in audioData, where i write
what I obtain from the MIC, I always get a value of '2'(+-1) in odd
index in audioData[], and I don't understand why, Maybe because I'm
using 16_bit_PCM??
Could you make some comment about my code?? I would appreciate that a
lot.
--
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