Android 音频问题汇总

最近界面里要添加语音功能,记录下做的过程中遇到的问题:

1、java.io.IOException: Prepare failed.: status=0x1

解决方法:

mp.setDataSource(PATH_TO_FILE);
mp.prepare();
这个地方修改如下:
File file = new File(strPath);
FileInputStream fis = new FileInputStream(file);
mediaPlayer.setDataSource(fis.getFD());
mp.prepare();
或者
把mediaPlayer.prepare;改成
mediaPlayer.prepareAsync();
这种情况会报个prepare called in state 8错误

 

2、录制音频时使用的是AudioRecorder,因为它比MediaRecorder功能更强,可塑造性高,但是它默认录制出来的音频格式为RAW,也就是原始音频格式,Android不能播放,目前尝试过的有两种方法:

①、转换为WAV,网上有个好的音频录制工具类ExtAudioRecorder,录制出来的音频文件挺清晰的并且转为为了WAV格式,它采用的是将WAV的头信息写入到音频文件中

// write file header
randomAccessWriter = new RandomAccessFile(filePath, "rw");
randomAccessWriter.setLength(0); // Set file length to 0, to prevent unexpected behavior in case the file already existed
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

  ②、由于项目中需要对音频进行转码,要转成MP3格式,所以就没有采用第一种方案,直接使用的JNI调用的Lame,一步到位,跟网上其他的差不多,就是自己加了个返回值判断转码是否成功,搞定

 

posted @ 2015-07-02 17:06  Assasin哲  阅读(878)  评论(0编辑  收藏  举报