Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

android - Audio Appending using RandomAccessFile

I use the following code to append as many wav files present in the sdcard to a single file. audFullPath is an arraylist containing the path of the audiofiles. Is it correct. When I play the recordedaudio1, after doing this. It play only the first file. I want to play all the files. Any suggestion..

File file=new File("/sdcard/AudioRecorder/recordedaudio1.wav");

RandomAccessFile raf = new RandomAccessFile(file, "rw");

for(int i=0;i<audFullPath.size();i++) {
    f=new File(audFullPath.get(i));      
    fileContent = new byte[(int)f.length()];
    System.out.println("Filecontent"+fileContent);
    raf.seek(raf.length()); 
    raf.writeBytes(audFullPath.get(i));
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can't append WAV files the way you do. That's because each WAV has special format:

The simplest possible WAV file looks like this:

[RIFF HEADER]
...
totalFileSize    

[FMT CHUNK]
...
audioFormat
frequency
bytesPerSample
numberOfChannels
...

[DATA CHUNK]
dataSize
<audio data>

What you need to do is:

  1. Make sure that all WAV files are of compatible: same audioFormat, frequency, bits per sample, number of channels, etc.
  2. Create proper RIFF header with total file size
  3. Create proper FMT header
  4. Create proper DATA header with total audio data size

This algorithm will definitely work for LPCM, ULAW, ALAW audio formats. Not sure about others.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...