Welcome to OGeek Q&A 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...