Android Record raw bytes into WAVE file for Http S

2019-03-22 05:53发布

问题:

So I am using AudioRecord from Android to record raw bytes and for writing them into a .wav file. Since Android has no support for this I had to write the .wav file headers manually with the following code:

    randomAccessWriter.writeBytes("RIFF");
    randomAccessWriter.writeInt(0); // Final file size not known yet, write 0 
    randomAccessWriter.writeBytes("WAVE");
    randomAccessWriter.writeBytes("fmt ");
    randomAccessWriter.writeInt(Integer.reverseBytes(16)); // Sub-chunk size, 16 for PCM
    randomAccessWriter.writeShort(Short.reverseBytes((short) 1)); // AudioFormat, 1 for PCM
    randomAccessWriter.writeShort(Short.reverseBytes(nChannels));// Number of channels, 1 for mono, 2 for stereo
    randomAccessWriter.writeInt(Integer.reverseBytes(sRate)); // Sample rate
    randomAccessWriter.writeInt(Integer.reverseBytes(sRate*bSamples*nChannels/8)); // Byte rate, SampleRate*NumberOfChannels*BitsPerSample/8
    randomAccessWriter.writeShort(Short.reverseBytes((short)(nChannels*bSamples/8))); // Block align, NumberOfChannels*BitsPerSample/8
    randomAccessWriter.writeShort(Short.reverseBytes(bSamples)); // Bits per sample
    randomAccessWriter.writeBytes("data");
    randomAccessWriter.writeInt(0); // Data chunk size not known yet, write 0

And at the end I write the length of the file:

   randomAccessWriter.seek(4); // Write size to RIFF header
   randomAccessWriter.writeInt(Integer.reverseBytes(36+payloadSize));

The code works and the .Wav file can be played in Winamp, on a WebPlayer, and even on Android if the file is stored on the SDCard or inisde the application.

THE PROBLEM: I want to stream this file on an iPhone or Android phone using HTTP Streaming and it does not work. The MediaPlayer on Android gives me the following error:

  MediaPlayer : error(1,-2147483648)

Do I need to write some extra data/headers in the file to allow it to stream properly ?

I would highly appreciate your help! Thanks.