所以我使用Android的AudioRecord
来记录原始字节并将它们写入.wav文件。由于Android不支持此操作,因此我必须使用以下代码手动编写.wav文件头:
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
最后我写了文件的长度:
randomAccessWriter.seek(4); // Write size to RIFF header
randomAccessWriter.writeInt(Integer.reverseBytes(36+payloadSize));
代码可以工作,.Wav文件可以在Winamp,WebPlayer上播放,甚至可以在Android上播放,如果文件存储在SDCard上或者是应用程序中。
问题:我想使用HTTP Streaming在iPhone或Android手机上传输此文件,但它不起作用。 Android上的MediaPlayer
给出了以下错误:
MediaPlayer : error(1,-2147483648)
我是否需要在文件中写一些额外的数据/标题才能正确传输?
我非常感谢你的帮助!感谢。