static final int BUFFER_SIZE = AudioRecord.getMinBufferSize(SAMPLE_RATE_IN_HZ,
AudioFormat.CHANNEL_IN_DEFAULT, AudioFormat.ENCODING_PCM_16BIT);
/**
* 开始录音
*/
public boolean startRecording() {
/**
* 申请音频焦点
*/
//isFocusGain = requestAudioFocus();
//if (isFocusGain) {
// 如果正在录音,则返回
if (isRecording) {
return isRecording;
}
// 初始化
if (mRecorder == null) {
initRecorder();
}
getFilePath();
try {
mRecorder.startRecording();
} catch (IllegalStateException e) {
ToastUtils.showToast(activity,"wuquanxian");
isRecording=false;
mRecorder=null;
e.printStackTrace();
}
// Log.i("weiz===",
// "mRecorder.getState():::"+mRecorder.getState()+"mRecorder.RECORDSTATE_RECORDING::"+mRecorder.RECORDSTATE_RECORDING);
// 如果存在文件
File recordFile = new File(rawPath);
if (recordFile.exists()) {
appendfile(recordFile);
} else {
// 初始化录音时间
// mRecordTime = 0;
startBufferedWrite(new File(rawPath));
}
isRecording = true;
//}else{
// ToastUtils.showToast(activity, "获取麦克风使用权限失败,请重试!");
//}
return isRecording;
}
/**
* 追加内容raw文件
*
* */
private boolean isappendFilst;
private void appendfile(final File recordFile){
isappendFilst=true;
isStop=false;
new Thread(new Runnable() {
@Override
public void run() {
if(recordFile.exists()){
try {
// 打开一个随机访问文件流,按读写方式
randomFile = new RandomAccessFile(recordFile, "rw");
// 文件长度,字节数
long fileLength = randomFile.length();
//将写文件指针移到文件尾。
randomFile.seek(fileLength);
while (isRecording) {
//mRecorder.RECORDSTATE_RECORDING==mRecorder.getState()
//Log.i("weiz===","222222222222");
int v = 0;
int readSize = 0;
try {
readSize = mRecorder.read(mBuffer, 0,
mBuffer.length);
Log.i("weiz===","----"+readSize);
} catch (Exception e1) {
Log.i("weiz===", "bbbbb"+readSize);
e1.printStackTrace();
}
for (int i = 0; i < readSize; i++) {
//文件写入
randomFile.writeShort(mBuffer[i]);
//获取音量
v += mBuffer[i] * mBuffer[i];
}
//没有停止
if(!isStop && readSize>0){
double mean =v/Math.abs(readSize);
double volume=10 * Math.log10(mean);
try {
//获取分贝
double vol=volume;
if(vol<30){
vol=0f;
}
if(isappendFilst){
isappendFilst=false;
}else{
Message msg =handler.obtainMessage();
msg.what=3;
msg.obj=vol;
handler.sendMessage(msg);
Thread.sleep(100);
//Log.i("weiz===", "vol::::"+vol);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//mRecorder.stop();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
randomFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}).start();
}
/**
* 停止录音,并且转换文件,<br/>
* <b>这很可能是个耗时操作,建议在后台中做
*
* @return
*/
public boolean stopRecordingAndConvertFile() {
if (!isRecording) {
return isRecording;
}
isStop =true;
Log.i("weiz====", "-----mRecorder----::::"+mRecorder);
// 停止
mRecorder.stop();
//Log.i("weiz===", "mRecorder.getState():::"+mRecorder.getState());
//释放资源
mRecorder.release();
mRecorder=null;
isRecording = false;
// 开始转换
FLameUtils lameUtils = new FLameUtils(1, SAMPLE_RATE, 96);
convertOk = lameUtils.raw2mp3(rawPath, mp3Path);
//mAudioControl.abandonAudioFocus(afChangeListener);
return isRecording ^ convertOk;// convertOk==true,return true
}
/**
*
* 文件转化
* **/
public boolean recordConvertFile(){
// 开始转换
FLameUtils lameUtils = new FLameUtils(1, SAMPLE_RATE, 96);
convertOk = lameUtils.raw2mp3(rawPath, mp3Path);
return convertOk;
}
/**
* 停止录音
*
* */
public boolean stopRecording(){
if (!isRecording) {
return isRecording;
}
isStop =true;
Log.i("weiz====", "-----mRecorder----::::"+mRecorder);
// 停止
mRecorder.stop();
//Log.i("weiz===", "mRecorder.getState():::"+mRecorder.getState());
//释放资源
mRecorder.release();
mRecorder=null;
isRecording = false;
//释放焦点
//mAudioControl.abandonAudioFocus(afChangeListener);
return isRecording;
}
/**
*
* 暂停,不转化文件,记录文件路径
* 返回文件路径
*
* **/
public String puaseRecordingAadSaveFile(){
if (!isRecording) {
return "isRecording";
}
isStop =true;
// 停止
if(null!=mRecorder){
mRecorder.stop();
//Log.i("weiz===", "mRecorder.getState():::"+mRecorder.getState());
//释放资源
mRecorder.release();
mRecorder=null;
}
isRecording = false;
//释放焦点
//mAudioControl.abandonAudioFocus(afChangeListener);
return rawPath;
}
/**
* 文件转化
*
* */
public boolean changeMp3(){
// 开始转换
FLameUtils lameUtils = new FLameUtils(1, SAMPLE_RATE, 96);
convertOk = lameUtils.raw2mp3(rawPath, mp3Path);
return convertOk;
}
用aduioRecored录音完成后,转为MP3播放的语速比录音的快好多。
请教各位这个是什么情况~