I have written following code in which I am trying to record and play the audio... But all I get is just the noise... Please help ...... I also want to know why my UI thread hangs when I have a separate thread for recording.....Thanks in advance..
/** * @author amit * */ public class AudioRecorder extends Activity { private String LOG_TAG = null; /* variables which are required to generate and manage the UI of the App */ // private RecordButton mRecordButton = null; private Button recordBtn, stopBtn, playBtn; /* * variables which are required for the actual functioning of the recording * and playing */ private AudioRecord recorder = null; private AudioTrack player = null; private int recorderBufSize, recordingSampleRate; private int trackBufSize; private byte[] audioData; private boolean isRecording = false, isPlaying = false; private Thread startRecThread; private AudioRecord.OnRecordPositionUpdateListener posUpdateListener; /** * constructor method for initializing the variables */ public AudioRecorder() { super(); LOG_TAG = "Constructor"; recorderBufSize = recordingSampleRate = trackBufSize = 0; // init function will initialize all the necessary variables ... init(); if (recorder != null && player != null) { Log.e(LOG_TAG, "recorder and player initialized"); audioData = new byte[recorderBufSize]; } else { Log.e(LOG_TAG, "Problem inside init function "); } posUpdateListener = new AudioRecord.OnRecordPositionUpdateListener() { int bytesRead = 0; @Override public void onPeriodicNotification(AudioRecord recorder) { // TODO Auto-generated method stub // Log.e(LOG_TAG, "inside position listener"); bytesRead = recorder.read(audioData, 0, audioData.length); player.write(audioData, 0, bytesRead); } @Override public void onMarkerReached(AudioRecord recorder) { // TODO Auto-generated method stub Log.e(LOG_TAG, "Marker Reached"); } }; // listener will be called every time 160 frames are reached recorder.setPositionNotificationPeriod(160); recorder.setRecordPositionUpdateListener(posUpdateListener); startRecThread = new Thread() { @Override public void run() { // TODO Auto-generated method stub recorder.startRecording(); // Log.e(LOG_TAG, "running"); // while (recorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) { recorder.read(audioData, 0, recorderBufSize); // } } }; Log.e(LOG_TAG, "inside constructor"); } private void init() { LOG_TAG = "initFunc"; int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 }; short audioFormat = AudioFormat.ENCODING_PCM_16BIT; for (int rate : mSampleRates) { try { Log.d(LOG_TAG, "Attempting rate " + rate + "Hz, bits: " + audioFormat); int bufrSize = AudioRecord.getMinBufferSize(rate, AudioFormat.CHANNEL_IN_MONO, audioFormat); if (bufrSize != AudioRecord.ERROR_BAD_VALUE && bufrSize != AudioRecord.ERROR) { // check if we can instantiate and have a success AudioRecord rec = new AudioRecord( MediaRecorder.AudioSource.MIC, rate, AudioFormat.CHANNEL_IN_MONO, audioFormat, bufrSize); if (rec != null && rec.getState() == AudioRecord.STATE_INITIALIZED) { // storing variables for future use . . . this.recordingSampleRate = rate; this.recorderBufSize = bufrSize; Log.e(LOG_TAG, "Returning..(rate:channelConfig:audioFormat:bufferSize)" + rate + ":" + AudioFormat.CHANNEL_IN_MONO + ":" + audioFormat + ":" + bufrSize); // Now create an instance of the AudioTrack int audioTrackBufSize = AudioTrack .getMinBufferSize(rate, AudioFormat.CHANNEL_OUT_MONO, audioFormat); Log.e(LOG_TAG, "AudioTrack buf size :" + audioTrackBufSize); this.trackBufSize = audioTrackBufSize; this.player = new AudioTrack(AudioManager.STREAM_MUSIC, rate, AudioFormat.CHANNEL_OUT_MONO, audioFormat, audioTrackBufSize, AudioTrack.MODE_STREAM); this.recorder = rec; return; } } } catch (IllegalArgumentException e) { Log.d(LOG_TAG, rate + "Exception, keep trying.", e); } catch (Exception e) { Log.e(LOG_TAG, rate + "Some Exception!!", e); } // for loop for channel config ended here. . . . // for loop for audioFormat ended here. . . } return; } private void startPlaying() { LOG_TAG = "startPlaying"; Log.e(LOG_TAG, "start Playing"); } private void stopPlaying() { LOG_TAG = "stopPlaying"; Log.e(LOG_TAG, "stop Playing"); } private void startRecording() { LOG_TAG = "startRecording"; player.play(); /* start a separate recording thread from here . . . */ startRecThread.start(); // recorder.read(audioData, 0, recorderBufSize); Log.e(LOG_TAG, "start Recording"); } private void stopRecording() { LOG_TAG = "stopRecording"; if(startRecThread.isAlive()) startRecThread.stop(); recorder.stop(); player.stop(); Log.e(LOG_TAG, "stop Recording"); } private void stop() { if (isRecording) { isRecording = false; stopRecording(); } if (isPlaying) { isPlaying = false; stopPlaying(); } recordBtn.setEnabled(true); playBtn.setEnabled(true); } @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); LinearLayout ll = new LinearLayout(this); // creating Buttons one by one . . . . // button to start the recording process recordBtn = new Button(this); recordBtn.setText("Record"); recordBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub recordBtn.setEnabled(false); playBtn.setEnabled(false); isRecording = true; startRecording(); } }); // single button to stop recording and playing as applicable stopBtn = new Button(this); stopBtn.setText("Stop"); stopBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub stop(); } }); // button to play the recorded sound playBtn = new Button(this); playBtn.setText("Play"); playBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // reverse the isPlaying isPlaying = true; recordBtn.setEnabled(false); playBtn.setEnabled(false); startPlaying(); } }); ll.addView(recordBtn, new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1)); ll.addView(playBtn, new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1)); ll.addView(stopBtn, new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1)); setContentView(ll); } @Override protected void onDestroy() { // Clean up code . .. super.onDestroy(); if(startRecThread.isAlive()) startRecThread.destroy(); if (recorder != null) recorder.release(); if (player != null) player.release(); startRecThread = null; recorder = null; player = null; recordBtn = null; stopBtn = null; playBtn = null; audioData = null; System.gc(); } } -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en