赞
踩
package com.badao.audiodemo.audioHelper;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import java.io.File;public classAudioRecordManager implements Handler.Callback {private static final String TAG = "LQR_AudioRecordManager";private intRECORD_INTERVAL;privateString SAVE_PATH;privateIAudioState mCurAudioState;privateContext mContext;privateHandler mHandler;privateAudioManager mAudioManager;privateMediaRecorder mMediaRecorder;privateUri mAudioPath;private longsmStartRecTime;privateAudioManager.OnAudioFocusChangeListener mAfChangeListener;
IAudioState idleState;
IAudioState recordState;
IAudioState sendingState;
IAudioState cancelState;
IAudioState timerState;privateIAudioRecordListener mAudioRecordListener;public staticAudioRecordManager mInstance;public staticAudioRecordManager getInstance(Context context) {if (mInstance == null) {
Class var1= AudioRecordManager.class;
synchronized(AudioRecordManager.class) {if (mInstance == null) {
mInstance= newAudioRecordManager(context);
}
}
}returnmInstance;
}
@TargetApi(21)privateAudioRecordManager(Context context) {this.mContext =context;this.mHandler = new Handler(this);this.RECORD_INTERVAL = 60;this.idleState = newAudioRecordManager.IdleState();this.recordState = newAudioRecordManager.RecordState();this.sendingState = newAudioRecordManager.SendingState();this.cancelState = newAudioRecordManager.CancelState();this.timerState = newAudioRecordManager.TimerState();if (Build.VERSION.SDK_INT < 21) {try{
TelephonyManager e= (TelephonyManager)this.mContext.getSystemService("phone");
e.listen(newPhoneStateListener() {public void onCallStateChanged(intstate, String incomingNumber) {switch(state) {case 1:
AudioRecordManager.this.sendEmptyMessage(6);case 0:case 2:default:
super.onCallStateChanged(state, incomingNumber);
}
}
},32);
}catch(SecurityException var3) {
var3.printStackTrace();
}
}this.mCurAudioState = this.idleState;this.idleState.enter();
}publicfinal boolean handleMessage(Message msg) {
Log.i("LQR_AudioRecordManager", "handleMessage" +msg.what);
AudioStateMessage m;switch(msg.what) {case 2:this.sendEmptyMessage(2);break;case 7:
m=AudioStateMessage.obtain();
m.what=msg.what;
m.obj=msg.obj;this.sendMessage(m);break;case 8:
m=AudioStateMessage.obtain();
m.what= 7;
m.obj=msg.obj;this.sendMessage(m);
}return false;
}private voidinitView() {if (this.mAudioRecordListener != null) {this.mAudioRecordListener.initTipView();
}
}private void setTimeoutView(intcounter) {if (this.mAudioRecordListener != null) {this.mAudioRecordListener.setTimeoutTipView(counter);
}
}private voidsetRecordingView() {if (this.mAudioRecordListener != null) {this.mAudioRecordListener.setRecordingTipView();
}
}private voidsetCancelView() {if (this.mAudioRecordListener != null) {this.mAudioRecordListener.setCancelTipView();
}
}private voiddestroyView() {
Log.d("LQR_AudioRecordManager", "destroyTipView");this.mHandler.removeMessages(7);this.mHandler.removeMessages(8);this.mHandler.removeMessages(2);if (this.mAudioRecordListener != null) {this.mAudioRecordListener.destroyTipView();
}
}public void setMaxVoiceDuration(intmaxVoiceDuration) {this.RECORD_INTERVAL =maxVoiceDuration;
}public voidsetAudioSavePath(String path) {if(TextUtils.isEmpty(path)) {this.SAVE_PATH = this.mContext.getCacheDir().getAbsolutePath();
}else{this.SAVE_PATH =path;
}
}public intgetMaxVoiceDuration() {return this.RECORD_INTERVAL;
}public voidstartRecord() {this.mAudioManager = (AudioManager)this.mContext.getSystemService("audio");if (this.mAfChangeListener != null) {this.mAudioManager.abandonAudioFocus(this.mAfChangeListener);this.mAfChangeListener = null;
}this.mAfChangeListener = newAudioManager.OnAudioFocusChangeListener() {public void onAudioFocusChange(intfocusChange) {
Log.d("LQR_AudioRecordManager", "OnAudioFocusChangeListener" +focusChange);if (focusChange == -1) {
AudioRecordManager.this.mAudioManager.abandonAudioFocus(AudioRecordManager.this.mAfChangeListener);
AudioRecordManager.this.mAfChangeListener = null;
AudioRecordManager.this.sendEmptyMessage(6);
}
}
};this.sendEmptyMessage(1);if (this.mAudioRecordListener != null) {this.mAudioRecordListener.onStartRecord();
}
}public voidwillCancelRecord() {this.sendEmptyMessage(3);
}public voidcontinueRecord() {this.sendEmptyMessage(4);
}public voidstopRecord() {this.sendEmptyMessage(5);
}public voiddestroyRecord() {
AudioStateMessage msg= newAudioStateMessage();
msg.obj= true;
msg.what= 5;this.sendMessage(msg);
}voidsendMessage(AudioStateMessage message) {this.mCurAudioState.handleMessage(message);
}void sendEmptyMessage(int event) {
AudioStateMessage message=AudioStateMessage.obtain();
message.what= event;this.mCurAudioState.handleMessage(message);
}private voidstartRec() {
Log.d("LQR_AudioRecordManager", "startRec");try{this.muteAudioFocus(this.mAudioManager, true);this.mAudioManager.setMode(0);this.mMediaRecorder = newMediaRecorder();try{int bps = 7950;this.mMediaRecorder.setAudioSamplingRate(8000);this.mMediaRecorder.setAudioEncodingBitRate(bps);
}catch(Resources.NotFoundException var2) {
var2.printStackTrace();
}this.mMediaRecorder.setAudioChannels(1);this.mMediaRecorder.setAudioSource(1);this.mMediaRecorder.setOutputFormat(3);this.mMediaRecorder.setAudioEncoder(1);this.mAudioPath = Uri.fromFile(new File(this.SAVE_PATH, System.currentTimeMillis() + "temp.voice"));this.mMediaRecorder.setOutputFile(this.mAudioPath.getPath());this.mMediaRecorder.prepare();this.mMediaRecorder.start();
Message e1=Message.obtain();
e1.what= 7;
e1.obj= 10;this.mHandler.sendMessageDelayed(e1, (long)(this.RECORD_INTERVAL * 1000 - 10000));
}catch(Exception var3) {
var3.printStackTrace();
}
}privateboolean checkAudioTimeLength() {long delta = SystemClock.elapsedRealtime() - this.smStartRecTime;return delta < 1000L;
}private voidstopRec() {
Log.d("LQR_AudioRecordManager", "stopRec");try{this.muteAudioFocus(this.mAudioManager, false);if (this.mMediaRecorder != null) {this.mMediaRecorder.stop();this.mMediaRecorder.release();this.mMediaRecorder = null;
}
}catch(Exception var2) {
var2.printStackTrace();
}
}private voiddeleteAudioFile() {
Log.d("LQR_AudioRecordManager", "deleteAudioFile");if (this.mAudioPath != null) {
File file= new File(this.mAudioPath.getPath());if(file.exists()) {
file.delete();
}
}
}private voidfinishRecord() {
Log.d("LQR_AudioRecordManager", "finishRecord path =" + this.mAudioPath);if (this.mAudioRecordListener != null) {int duration = (int)(SystemClock.elapsedRealtime() - this.smStartRecTime) / 1000;this.mAudioRecordListener.onFinish(this.mAudioPath, duration);
}
}private voidaudioDBChanged() {if (this.mMediaRecorder != null) {int db = this.mMediaRecorder.getMaxAmplitude() / 600;if (this.mAudioRecordListener != null) {this.mAudioRecordListener.onAudioDBChanged(db);
}
}
}private voidmuteAudioFocus(AudioManager audioManager, boolean bMute) {if (Build.VERSION.SDK_INT < 8) {
Log.d("LQR_AudioRecordManager", "muteAudioFocus Android 2.1 and below can not stop music");
}else if(bMute) {
audioManager.requestAudioFocus(this.mAfChangeListener, 3, 2);
}else{
audioManager.abandonAudioFocus(this.mAfChangeListener);this.mAfChangeListener = null;
}
}publicIAudioRecordListener getAudioRecordListener() {return this.mAudioRecordListener;
}public voidsetAudioRecordListener(IAudioRecordListener audioRecordListener) {this.mAudioRecordListener =audioRecordListener;
}classIdleState extends IAudioState {publicIdleState() {
Log.d("LQR_AudioRecordManager", "IdleState");
}voidenter() {
super.enter();if (AudioRecordManager.this.mHandler != null) {
AudioRecordManager.this.mHandler.removeMessages(7);
AudioRecordManager.this.mHandler.removeMessages(8);
AudioRecordManager.this.mHandler.removeMessages(2);
}
}voidhandleMessage(AudioStateMessage msg) {
Log.d("LQR_AudioRecordManager", "IdleState handleMessage :" +msg.what);switch(msg.what) {case 1:
AudioRecordManager.this.initView();
AudioRecordManager.this.setRecordingView();
AudioRecordManager.this.startRec();
AudioRecordManager.this.smStartRecTime =SystemClock.elapsedRealtime();
AudioRecordManager.this.mCurAudioState = AudioRecordManager.this.recordState;
AudioRecordManager.this.sendEmptyMessage(2);default:
}
}
}classRecordState extends IAudioState {
RecordState() {
}voidhandleMessage(AudioStateMessage msg) {
Log.d("LQR_AudioRecordManager", this.getClass().getSimpleName() + "handleMessage :" +msg.what);switch(msg.what) {case 2:
AudioRecordManager.this.audioDBChanged();
AudioRecordManager.this.mHandler.sendEmptyMessageDelayed(2, 150L);break;case 3:
AudioRecordManager.this.setCancelView();
AudioRecordManager.this.mCurAudioState = AudioRecordManager.this.cancelState;case 4:default:break;case 5:
final booleanchecked = AudioRecordManager.this.checkAudioTimeLength();
boolean activityFinished= false;if (msg.obj != null) {
activityFinished=(Boolean)msg.obj;
}if (checked && !activityFinished) {if (AudioRecordManager.this.mAudioRecordListener != null) {
AudioRecordManager.this.mAudioRecordListener.setAudioShortTipView();
}
AudioRecordManager.this.mHandler.removeMessages(2);
}if (!activityFinished && AudioRecordManager.this.mHandler != null) {
AudioRecordManager.this.mHandler.postDelayed(newRunnable() {public voidrun() {
AudioStateMessage message=AudioStateMessage.obtain();
message.what= 9;
message.obj= !checked;
AudioRecordManager.this.sendMessage(message);
}
},500L);
AudioRecordManager.this.mCurAudioState = AudioRecordManager.this.sendingState;
}else{
AudioRecordManager.this.stopRec();if (!checked &&activityFinished) {
AudioRecordManager.this.finishRecord();
}
AudioRecordManager.this.destroyView();
AudioRecordManager.this.mCurAudioState = AudioRecordManager.this.idleState;
}break;case 6:
AudioRecordManager.this.stopRec();
AudioRecordManager.this.destroyView();
AudioRecordManager.this.deleteAudioFile();
AudioRecordManager.this.mCurAudioState = AudioRecordManager.this.idleState;
AudioRecordManager.this.idleState.enter();break;case 7:int counter =(Integer)msg.obj;
AudioRecordManager.this.setTimeoutView(counter);
AudioRecordManager.this.mCurAudioState = AudioRecordManager.this.timerState;if (counter > 0) {
Message message=Message.obtain();
message.what= 8;
message.obj= counter - 1;
AudioRecordManager.this.mHandler.sendMessageDelayed(message, 1000L);
}else{
AudioRecordManager.this.mHandler.postDelayed(newRunnable() {public voidrun() {
AudioRecordManager.this.stopRec();
AudioRecordManager.this.finishRecord();
AudioRecordManager.this.destroyView();
}
},500L);
AudioRecordManager.this.mCurAudioState = AudioRecordManager.this.idleState;
}
}
}
}classSendingState extends IAudioState {
SendingState() {
}voidhandleMessage(AudioStateMessage message) {
Log.d("LQR_AudioRecordManager", "SendingState handleMessage" +message.what);switch(message.what) {case 9:
AudioRecordManager.this.stopRec();if((Boolean)message.obj) {
AudioRecordManager.this.finishRecord();
}
AudioRecordManager.this.destroyView();
AudioRecordManager.this.mCurAudioState = AudioRecordManager.this.idleState;default:
}
}
}classCancelState extends IAudioState {
CancelState() {
}voidhandleMessage(AudioStateMessage msg) {
Log.d("LQR_AudioRecordManager", this.getClass().getSimpleName() + "handleMessage :" +msg.what);switch(msg.what) {case 1:case 2:case 3:default:break;case 4:
AudioRecordManager.this.setRecordingView();
AudioRecordManager.this.mCurAudioState = AudioRecordManager.this.recordState;
AudioRecordManager.this.sendEmptyMessage(2);break;case 5:case 6:
AudioRecordManager.this.stopRec();
AudioRecordManager.this.destroyView();
AudioRecordManager.this.deleteAudioFile();
AudioRecordManager.this.mCurAudioState = AudioRecordManager.this.idleState;
AudioRecordManager.this.idleState.enter();break;case 7:int counter =(Integer)msg.obj;if (counter > 0) {
Message message=Message.obtain();
message.what= 8;
message.obj= counter - 1;
AudioRecordManager.this.mHandler.sendMessageDelayed(message, 1000L);
}else{
AudioRecordManager.this.mHandler.postDelayed(newRunnable() {public voidrun() {
AudioRecordManager.this.stopRec();
AudioRecordManager.this.finishRecord();
AudioRecordManager.this.destroyView();
}
},500L);
AudioRecordManager.this.mCurAudioState = AudioRecordManager.this.idleState;
AudioRecordManager.this.idleState.enter();
}
}
}
}classTimerState extends IAudioState {
TimerState() {
}voidhandleMessage(AudioStateMessage msg) {
Log.d("LQR_AudioRecordManager", this.getClass().getSimpleName() + "handleMessage :" +msg.what);switch(msg.what) {case 3:
AudioRecordManager.this.setCancelView();
AudioRecordManager.this.mCurAudioState = AudioRecordManager.this.cancelState;case 4:default:break;case 5:
AudioRecordManager.this.mHandler.postDelayed(newRunnable() {public voidrun() {
AudioRecordManager.this.stopRec();
AudioRecordManager.this.finishRecord();
AudioRecordManager.this.destroyView();
}
},500L);
AudioRecordManager.this.mCurAudioState = AudioRecordManager.this.idleState;
AudioRecordManager.this.idleState.enter();break;case 6:
AudioRecordManager.this.stopRec();
AudioRecordManager.this.destroyView();
AudioRecordManager.this.deleteAudioFile();
AudioRecordManager.this.mCurAudioState = AudioRecordManager.this.idleState;
AudioRecordManager.this.idleState.enter();break;case 7:int counter =(Integer)msg.obj;if (counter > 0) {
Message message=Message.obtain();
message.what= 8;
message.obj= counter - 1;
AudioRecordManager.this.mHandler.sendMessageDelayed(message, 1000L);
AudioRecordManager.this.setTimeoutView(counter);
}else{
AudioRecordManager.this.mHandler.postDelayed(newRunnable() {public voidrun() {
AudioRecordManager.this.stopRec();
AudioRecordManager.this.finishRecord();
AudioRecordManager.this.destroyView();
}
},500L);
AudioRecordManager.this.mCurAudioState = AudioRecordManager.this.idleState;
}
}
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。