I want to use an AudioTrack to play a sound and I recorded it using AudioRecord at the same time. But I cant get anything except zero. Why?
I have tried to read data from the byte[]
buffer and all I get is zero. Here is my code:
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Time;
import java.util.Timer;
import java.util.TimerTask;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
public class RecordThread implements Runnable {
private static final int Encording = AudioFormat.ENCODING_PCM_16BIT; //Data size for each frame = 16 bytes
private static final int Sample_rate = 8000; //Sample rate = 8000 HZ
private static final int Channel = AudioFormat.CHANNEL_IN_MONO; //Set as single track
public static final int Buffersize = AudioRecord.getMinBufferSize(Sample_rate, Channel, Encording); //Buffer size
@Override
public void run() {
// TODO Auto-generated method stub
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,Sample_rate, Channel, Encording, Buffersize);
while(MessageUsed.RecordPermit){
try {
FileOutputStream ops = new FileOutputStream(TestoOFDMMain.file2);
BufferedOutputStream bos = new BufferedOutputStream(ops);
DataOutputStream dos = new DataOutputStream(bos);
byte[] buffer = new byte[Buffersize];
audioRecord.startRecording(); //Start Recording
int bufferReadResult = audioRecord.read(buffer, 0, buffer.length); //Read Array size
MessageUsed.RecordStop = false;
/*Write Data to file*/
for(int i=0;i<bufferReadResult;i++){
dos.write(buffer[i]);
System.out.println(buffer[i]);
if(MessageUsed.ProcessPermit == true){
break;
}
}
audioRecord.stop();
dos.close();
MessageUsed.RecordStop = true;
Thread.sleep(300);
MessageUsed.ProcessStart = true;
Thread.sleep(900);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
I would assume on a quick glance that your System.out.println()
statement is not allowing the result to appear. That's a basic java expression- not Android java. The 0 you are seeing could be the default value of whatever field you are looking at.