我正在尝试使用Android中的AudioTrack播放存储在文本文件中的字节数组。字节数组由使用AudioDispatcher(来自TarsosDSP库)的记录形成。我需要多次记录,因此,我将字节数组附加到同一文本文件中。录制完成后,我无法播放整个录制的字节文件(由多个录制的字节文件组成)。但是,我能够从单个录音中播放字节数组。
以下是使用AudioTrack播放字节文件的代码
byte bytePlay[] = FileUtils.readFileToByteArray(file1);
Log.i("byte",bytePlay.length+"");
ShortBuffer sBuf = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
final short[] shorts = new short[sBuf.capacity()];
sBuf.get(shorts);
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
16000, //hard coded! not ideal
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT,
shorts.length,
AudioTrack.MODE_STREAM);
audioTrack.play();
// Write the music buffer to the AudioTrack object
audioTrack.write(shorts, 0, shorts.length);
}catch(Exception e){
e.printStackTrace();
}
以下是我将数据附加到文件
的代码 File file1 = new File(file_name);
if (!file1.exists()) {
try {
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file1,true);
fos.write(bytePlayRec);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
请帮帮我。提前谢谢。