问题描述:

I'm trying to play byte array stored in a text file using AudioTrack in android. The byte array is formed from the recording using AudioDispatcher (from TarsosDSP library). I'm required to record multiple times, hence, I'm appending the byte array to the same text file. After the recording is done,I'm not able to play the byte file for the entire recording (which consists of the byte file from the multiple recording). But, I'm able to play the byte array from a single recording.

Here is the code for playing the byte file using 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();

}

Here is the code where I'm appending data to the file

 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();

}

Please help me out. Thanks in advance.

相关阅读:
Top