为什么我从 audiorecord 的输出中只得到零?
Why do I get nothing but zero from the output of audiorecord?
我想用AudioTrack来播放声音,同时用AudioRecord录了下来。但除了零,我什么也得不到。为什么?
我试图从 byte[]
缓冲区读取数据,但我得到的只是零。这是我的代码:
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();
}
}
}
}
我会假设你的 System.out.println()
声明不允许结果出现。这是一个基本的 java 表达式,而不是 Android java。您看到的 0 可能是您正在查看的任何字段的默认值。
我想用AudioTrack来播放声音,同时用AudioRecord录了下来。但除了零,我什么也得不到。为什么?
我试图从 byte[]
缓冲区读取数据,但我得到的只是零。这是我的代码:
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();
}
}
}
}
我会假设你的 System.out.println()
声明不允许结果出现。这是一个基本的 java 表达式,而不是 Android java。您看到的 0 可能是您正在查看的任何字段的默认值。