Sto provando a registrare un po 'di voce utilizzando la classe AudioRecord e poi scriverlo sul file .pcm di output. Voglio che il mio programma continui a registrare fino a quando non viene premuto il pulsante stop. Sfortunatamente per quanto tempo sto registrando, la dimensione del file di output è sempre di 3528 byte e dura circa 20 ms. Sempre secondo Toolsoft Audio Tools, le proprietà di quel file sono: 44100Hz, 16 bit, stereo, anche se sto usando mono con frequenza di campionamento completamente diversa.AudioRecord - scrittura file PCM
Thread recordingThread;
boolean isRecording = false;
int audioSource = AudioSource.MIC;
int sampleRateInHz = 44100;
int channelConfig = AudioFormat.CHANNEL_IN_MONO;
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
byte Data[] = new byte[bufferSizeInBytes];
AudioRecord audioRecorder = new AudioRecord(audioSource,
sampleRateInHz,
channelConfig,
audioFormat,
bufferSizeInBytes);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void startRecording(View arg0) {
audioRecorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
public void run() {
String filepath = Environment.getExternalStorageDirectory().getPath();
FileOutputStream os = null;
try {
os = new FileOutputStream(filepath+"/record.pcm");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while(isRecording) {
audioRecorder.read(Data, 0, Data.length);
try {
os.write(Data, 0, bufferSizeInBytes);
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
recordingThread.start();
}
public void stopRecording(View arg0) {
if (null != audioRecorder) {
isRecording = false;
audioRecorder.stop();
audioRecorder.release();
audioRecorder = null;
recordingThread = null;
}
}
Posso chiedere gentilmente di dirmi cosa c'è che non va? Spero che la risposta non sara 'tutto cio' :)
Penso che il tuo codice IO potrebbe essere il problema. Prova a utilizzare Apache Commons IOUtils per copiare gli stream: http://commons.apache.org/io/api-release/org/apache/commons/io/IOUtils.html#copy(java.io.InputStream,%20java.io .OutputStream) –