Android Record raw bytes into WAVE file for Http Streaming

左心房为你撑大大i 提交于 2019-12-04 14:16:41

问题


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.

来源:https://stackoverflow.com/questions/7336570/android-record-raw-bytes-into-wave-file-for-http-streaming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!