Hi ~
I'm an android starter
I used MediaRecorder and MediaPlayer to record voice via mic before
This time,I try AudioRecord and AudioTrack to record voice
But when I launch the program, it's forced to be shut down
I've no idea where is wrong?
here are my code ~
Could you tell me why?
TKS in advance :)
public class audioRecorder extends Activity
{
Boolean recordFlag = true;
Button recordButton;
Button stopButton;
OnClickListener record_listener = null;
OnClickListener stop_listener = null;
AudioRecord record = null;
AudioTrack track = null;
protected static final int MENU_ABOUT = Menu.FIRST;
protected static final int MENU_Quit = Menu.FIRST + 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initResourceRefs();
record_listener = new OnClickListener()
{
@Override
public void onClick(View arg0)
{
record();
}
};//record_listener
stop_listener = new OnClickListener()
{
@Override
public void onClick(View arg0)
{
stopRecord();
}
};//stop_listener
setListeners();
}//onCreate
private void record()
{
int sampleRateHz = 11025;
int audioFormat = AudioFormat.CHANNEL_CONFIGURATION_MONO;
int audioEncodingFormat= MediaRecorder.AudioEncoder.AMR_NB;
//int audioEncodingFormat= AudioFormat.ENCODING_PCM_16BIT;
byte[] buffer = null;
int buffersize =
AudioRecord.getMinBufferSize(sampleRateHz,audioFormat,audioEncodingFormat);
buffer = new byte[buffersize];
record = new
AudioRecord(MediaRecorder.AudioSource.MIC,sampleRateHz,audioFormat,
audioEncodingFormat,buffersize);
track = new
AudioTrack(AudioManager.STREAM_VOICE_CALL,sampleRateHz,audioFormat,
audioEncodingFormat,buffersize,AudioTrack.MODE_STREAM);
track.setPlaybackRate(sampleRateHz);
record.startRecording();
track.play();
while(recordFlag)
{
record.read(buffer,0,buffersize);
track.write(buffer,0,buffer.length);
}
}//record
private void stopRecord()
{
recordFlag = false;
record.stop();
track.stop();
}//stopRecord
private void initResourceRefs()//初始化
{
recordButton = (Button)findViewById(R.id.recordButton);
stopButton = (Button)findViewById(R.id.stopButton);
}//initResourceRefs()
private void setListeners()
{
recordButton.setOnClickListener(record_listener);
stopButton.setOnClickListener(stop_listener);
}//setListeners
--
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