我们有一个长期运行的Android应用程序,可以在按下按钮时录制音频。全新安装后,录音按预期工作。
我让应用程序一夜之间运行,并在今天早上再次尝试录制。音频听起来像这样:
https://soundcloud.com/joe-mccall-7/our-android-app-one-through
我想也许应用程序让声音处理器处于奇怪的状态,所以我从Play商店安装了一个录音应用程序,这就是结果:
https://soundcloud.com/joe-mccall-7/audio-recorder-pro-one-through
切换回我们的应用并以相同的乱码音频录制结果。这是我们实例化录制对象的地方:
// the recording rate in samples per second (hz)
public static final int RECORDING_RATE = 11025;
// we will be recording and playing back as mono with PCM 16 bit encoding.
private static final int RECORDING_CHANNEL = AudioFormat.CHANNEL_IN_MONO;
private static final int ENCODING_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
// the buffer size is determined by the recording rate, channel and format
private static int RECORDING_BUFFER_SIZE = AudioRecord.getMinBufferSize(
RECORDING_RATE, RECORDING_CHANNEL, ENCODING_FORMAT) * 2;
// the audio recorder instance used for microphone input
private static AudioRecord recorder =
new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDING_RATE, RECORDING_CHANNEL, ENCODING_FORMAT,
RECORDING_BUFFER_SIZE);
稍后在代码中我们有一个长时间运行的线程,它从记录器实例读取音频并将其写入BufferedOutputStream
对象:
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filename);
// as long as the user has not canceled the streaming...
while (transmitting) {
// read the data into the buffer
int read = recorder.read(buffer, 0, buffer.length);
// write the buffer to the output stream
bos.write(buffer);
}
// close the buffer and write the wav file
考虑到全新安装的应用程序记录了几个音频文件,我不知道wav文件的编写问题是怎么回事。
当我强制停止应用并再次启动时,录音恢复正常:
https://soundcloud.com/joe-mccall-7/force-stop-and-restart-one-through-ten
那么你的理论是什么会导致wav文件变得像那样乱码?