Code is as follows:
public class MainActivity extends Activity {
/ ** Called when the activity is first created. * /
Button btnRecord, btnStop, btnExit;
SeekBar skbVolume ;/ / adjust the volume
boolean isRecording = false ;/ / if recording marks
static final int frequency = 8000;
static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
int recBufSize, playBufSize;
AudioRecord audioRecord;
AudioTrack audioTrack;
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
recBufSize = AudioRecord.getMinBufferSize (frequency,
channelConfiguration, audioEncoding);
playBufSize = AudioTrack.getMinBufferSize (frequency,
channelConfiguration, audioEncoding);
/ / -----------------------------------------
audioRecord = new AudioRecord (MediaRecorder.AudioSource.MIC, frequency,
channelConfiguration, audioEncoding, recBufSize);
audioTrack = new AudioTrack (AudioManager.STREAM_MUSIC, frequency,
channelConfiguration, audioEncoding, playBufSize,
AudioTrack.MODE_STREAM);
/ / ------------------------------------------
btnRecord = (Button) this.findViewById (R.id.btnRecord);
btnRecord.setOnClickListener (new ClickEvent ());
btnStop = (Button) this.findViewById (R.id.btnStop);
btnStop.setOnClickListener (new ClickEvent ());
btnExit = (Button) this.findViewById (R.id.btnExit);
btnExit.setOnClickListener (new ClickEvent ());
}
@ Override
protected void onDestroy () {
super.onDestroy ();
android.os.Process.killProcess (android.os.Process.myPid ());
}
class ClickEvent implements View.OnClickListener {
public void onClick (View v) {
if (v == btnRecord) {
isRecording = true;
new RecordPlayThread (). start () ;/ / open a thread-record side put
} Else if (v == btnStop) {
isRecording = false;
} Else if (v == btnExit) {
isRecording = false;
MainActivity.this.finish ();
}
}
} [Code = java] class RecordPlayThread extends Thread {
public void run () {
try {
byte [] buffer = new byte [recBufSize];
audioRecord.startRecording () ;/ / start recording
audioTrack.play () ;/ / start playing
while (isRecording) {
/ / Save the data to the buffer
from MICint size = recBufSize;
int bufferReadResult = audioRecord.read (buffer, 0,
recBufSize);
byte [] tmpBuf = new byte [bufferReadResult];
System.arraycopy (buffer, 0, tmpBuf, 0, bufferReadResult);
/ / Write data that is playing
audioTrack.write (tmpBuf, 0, tmpBuf.length);
}
audioTrack.stop ();
audioRecord.stop ();
} Catch (Throwable t) {
Toast.makeText (MainActivity.this, t.getMessage (), 1000);
}
}
};
} [/ Code]
Achieved record while playing side, the noise is very large, do not know how, knees and begged for help, Daniel
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
No comments:
Post a Comment