在項目開發(fā)中,有個需求:實現(xiàn)模仿微信錄音儒恋,發(fā)送語音的功能善绎。長按按鈕錄音,彈框顯示語音時間诫尽,以及上滑取消發(fā)送禀酱。我重寫了一個發(fā)送語音的控件,以實現(xiàn)該功能牧嫉。
首先添加權限:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
AudioRecorderButton剂跟,自己實現(xiàn)的自定義錄音控件
public class AudioRecorderButton extends Button {
private static final int STATE_NORMAL = 1;// 默認的狀態(tài)
private static final int STATE_RECORDING = 2;// 正在錄音
private static final int STATE_WANT_TO_CANCEL = 3;// 希望取消
private int mCurrentState = STATE_NORMAL; // 當前的狀態(tài)
private boolean isRecording = false;// 已經(jīng)開始錄音
private static final int DISTANCE_Y_CANCEL = 50;
private DialogManager mDialogManager;
private AudioManager mAudioManager;
// 是否觸發(fā)longClick
private boolean mReady;
android.media.AudioManager audioManager;
private static final int MSG_AUDIO_PREPARED = 0x110;
private static final int MSG_VOICE_CHANGED = 0x111;
private static final int MSG_DIALOG_DIMISS = 0x112;
private static final int MSG_TIME_OUT = 0x113;
private static final int UPDATE_TIME = 0x114;
private boolean mThreadFlag = false;
private int time = 0;
private float mTime;
/*
* 獲取音量大小的線程
*/
private Runnable mGetVoiceLevelRunnable = new Runnable() {
public void run() {
while (isRecording) {
try {
Thread.sleep(100);
mTime += 0.1f;
time++;
if (isWantToCancel) {
} else {
if (time % 10 == 0) {
mHandler.sendEmptyMessage(UPDATE_TIME);
}
}
mHandler.sendEmptyMessage(MSG_VOICE_CHANGED);
if (mTime >= 10.0f) {//如果時間超過10秒,自動結(jié)束錄音
while (!mThreadFlag) {//記錄已經(jīng)結(jié)束了錄音酣藻,不需要再次結(jié)束曹洽,以免出現(xiàn)問題
mDialogManager.dimissDialog();
mAudioManager.release();
if (audioFinishRecorderListener != null) {
//發(fā)消息給主線程,告訴他reset();
mHandler.sendEmptyMessage(MSG_TIME_OUT);
audioFinishRecorderListener.onFinish(mTime, mAudioManager.getCurrentFilePath());
}
mThreadFlag = !mThreadFlag;
}
isRecording = false;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_AUDIO_PREPARED:
// 顯示對話框在開始錄音以后
mDialogManager.showRecordingDialog();
isRecording = true;
// 開啟一個線程
new Thread(mGetVoiceLevelRunnable).start();
break;
case MSG_VOICE_CHANGED:
mDialogManager.updateVoiceLevel(mAudioManager.getVoiceLevel(7));
break;
case MSG_DIALOG_DIMISS:
mDialogManager.dimissDialog();
break;
case MSG_TIME_OUT://錄音超時
reset();
break;
case UPDATE_TIME://更新時間
if (time % 10 == 0) {
mDialogManager.updateTime(time / 10);
}
break;
}
}
};
/**
* 以下2個方法是構(gòu)造方法
*/
public AudioRecorderButton(Context context, AttributeSet attrs) {
super(context, attrs);
mDialogManager = new DialogManager(context);
audioManager = (android.media.AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
String dir = Environment.getExternalStorageDirectory().getPath() + "/" + LibraryContantsUtil.APP_NAME + "/audio";
mAudioManager = AudioManager.getInstance(dir);
mAudioManager.setOnAudioStateListener(new AudioManager.AudioStateListener() {
public void wellPrepared() {
if (!isOverDue) {
mHandler.sendEmptyMessage(MSG_AUDIO_PREPARED);//開啟線程
}
}
});
// 由于這個類是button所以在構(gòu)造方法中添加監(jiān)聽事件
setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
if (!isOverDue) {
mReady = true;
mAudioManager.prepareAudio();
}
return true;
}
});
}
public AudioRecorderButton(Context context) {
this(context, null);
}
/**
* 錄音完成后的回調(diào)
*/
public interface AudioFinishRecorderListener {
void onFinish(float seconds, String filePath);
}
private AudioFinishRecorderListener audioFinishRecorderListener;
public void setAudioFinishRecorderListener(AudioFinishRecorderListener listener) {
audioFinishRecorderListener = listener;
}
android.media.AudioManager.OnAudioFocusChangeListener afChangeListener = new android.media.AudioManager.OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT) {
// Pause playback
} else if (focusChange == android.media.AudioManager.AUDIOFOCUS_GAIN) {
// Resume playback
LogUtil.e("===", "+++");
} else if (focusChange == android.media.AudioManager.AUDIOFOCUS_LOSS) {
audioManager.abandonAudioFocus(afChangeListener);
// Stop playback
}
}
};
public void myRequestAudioFocus() {
audioManager.requestAudioFocus(afChangeListener, android.media.AudioManager.STREAM_MUSIC, android.media.AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
}
/**
* 屏幕的觸摸事件
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
// int x = (int) event.getX();// 獲得x軸坐標
float last = 0;// 獲得y軸坐標
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mThreadFlag = false;
changeState(STATE_RECORDING);
myRequestAudioFocus();
break;
case MotionEvent.ACTION_MOVE:
if (isRecording) {
// 如果想要取消辽剧,根據(jù)x,y的坐標看是否需要取消
if (event.getY() < 0 && Math.abs(event.getY()) > 120) {
changeState(STATE_WANT_TO_CANCEL);
} else {
changeState(STATE_RECORDING);
}
}
break;
case MotionEvent.ACTION_UP:
if (!mReady) {
reset();
return super.onTouchEvent(event);
}
if (!isRecording || mTime <= 1.0f) {//小于1秒
mDialogManager.tooShort();
mAudioManager.cancel();
mHandler.sendEmptyMessage(MSG_DIALOG_DIMISS);//顯示對話框
} else if (mCurrentState == STATE_RECORDING) { // 正在錄音的時候送淆,結(jié)束
mDialogManager.dimissDialog();
mAudioManager.release();
if (audioFinishRecorderListener != null) {
audioFinishRecorderListener.onFinish(mTime, mAudioManager.getCurrentFilePath());
}
} else if (mCurrentState == STATE_WANT_TO_CANCEL) { // 想要取消
mDialogManager.dimissDialog();
mAudioManager.cancel();
}
reset();
audioManager.abandonAudioFocus(afChangeListener);
}
break;
return super.onTouchEvent(event);
}
/**
* 恢復狀態(tài)及標志位
*/
private void reset() {
isRecording = false;
mTime = 0;
time = 1;
mReady = false;
changeState(STATE_NORMAL);
}
private boolean wantToCancle(int x, int y) {
if (y < 0) {
return true;
} else {
return false;
}
}
boolean isWantToCancel = false;
/**
* 改變
*/
private void changeState(int state) {
if (mCurrentState != state) {
mCurrentState = state;
switch (state) {
case STATE_NORMAL:
setBackgroundResource(R.drawable.shape_voice_chat_circle);
setText("按住說話");
break;
case STATE_RECORDING:
if (isRecording) {
mDialogManager.recording();
}
setBackgroundResource(R.drawable.shape_voice_chat_circle);
setText("松開結(jié)束");
isWantToCancel = false;
break;
case STATE_WANT_TO_CANCEL:
setBackgroundResource(R.drawable.shape_voice_chat_circle);
setText("松開手指,取消發(fā)送");
mDialogManager.wantToCancel();
isWantToCancel = true;
break;
}
}
}
}
下面是DialogManager抖仅,錄音時控制Dialog的顯示隱藏坊夫,以及需要Dialog中需要向用戶展示的內(nèi)容。
public class DialogManager {
private AlertDialog.Builder builder;
private ImageView mIcon;
// private ImageView mVoice;
private TextView mLable;
private Context context;
private AlertDialog dialog;//用于取消AlertDialog.Builder
/**
* 構(gòu)造方法 傳入上下文
*/
public DialogManager(Context context) {
this.context = context;
}
// 顯示錄音的對話框
public void showRecordingDialog() {
builder = new AlertDialog.Builder(context, R.style.voice_chat_dialog);
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.dialog_recorder, null);
mIcon = (ImageView) view.findViewById(R.id.id_recorder_dialog_icon);
mLable = (TextView) view.findViewById(R.id.id_recorder_dialog_label);
// GlideUtil.loadSquarePicture(R.drawable.gif_record, mIcon);
builder.setView(view);
builder.create();
dialog = builder.show();
dialog.setCanceledOnTouchOutside(false);//設置點擊外部不消失
}
public void recording() {
if (dialog != null && dialog.isShowing()) { //顯示狀態(tài)
mIcon.setVisibility(View.VISIBLE);
mLable.setVisibility(View.VISIBLE);
// GlideUtil.loadSquarePicture(R.drawable.gif_record, mIcon);
mIcon.setImageResource(R.mipmap.ic_record);
mLable.setText("手指上滑撤卢,取消發(fā)送");
}
}
// 顯示想取消的對話框
public void wantToCancel() {
if (dialog != null && dialog.isShowing()) { //顯示狀態(tài)
mIcon.setVisibility(View.VISIBLE);
mLable.setVisibility(View.VISIBLE);
mIcon.setImageResource(R.mipmap.ic_release_to_cancel);
mLable.setText("松開手指,取消發(fā)送");
}
}
public void updateTime(int time) {
if (dialog != null && dialog.isShowing()) { //顯示狀態(tài)
mIcon.setVisibility(View.VISIBLE);
mLable.setVisibility(View.VISIBLE);
mLable.setText(time + "s");
}
}
// 顯示時間過短的對話框
public void tooShort() {
if (dialog != null && dialog.isShowing()) { //顯示狀態(tài)
mIcon.setVisibility(View.VISIBLE);
mLable.setVisibility(View.VISIBLE);
mLable.setText("錄音時間過短");
}
}
// 顯示取消的對話框
public void dimissDialog() {
if (dialog != null && dialog.isShowing()) { //顯示狀態(tài)
dialog.dismiss();
dialog = null;
}
}
// 顯示更新音量級別的對話框
public void updateVoiceLevel(int level) {
if (dialog != null && dialog.isShowing()) { //顯示狀態(tài)
mIcon.setVisibility(View.VISIBLE);
mLable.setVisibility(View.VISIBLE);
// 在這里沒有做操作
//可以再這里根據(jù)音量大小梧兼,設置圖片放吩,實現(xiàn)效果;
}
}
}
下面是AudioManager羽杰,錄音類
public class AudioManager {
private MediaRecorder mMediaRecorder;
private String mDir;
private String mCurrentFilePath;
private static AudioManager mInstance;
private boolean isPrepare;
private AudioManager(String dir) {
mDir = dir;
}
public static AudioManager getInstance(String dir) {
if (mInstance == null) {
synchronized (AudioManager.class) {
if (mInstance == null) {
mInstance = new AudioManager(dir);
}
}
}
return mInstance;
}
/**
* 使用接口 用于回調(diào)
*/
public interface AudioStateListener {
void wellPrepared();
}
public AudioStateListener mAudioStateListener;
/**
* 回調(diào)方法
*/
public void setOnAudioStateListener(AudioStateListener listener) {
mAudioStateListener = listener;
}
// 去準備
public void prepareAudio() {
try {
isPrepare = false;
File dir = new File(mDir);
if (!dir.exists()) {
dir.mkdirs();
} else {
if (!dir.isDirectory()) {
dir.delete();
dir.mkdirs();
}
}
String fileName = generateFileName();
File file = new File(dir, fileName);
mCurrentFilePath = file.getAbsolutePath();
mMediaRecorder = new MediaRecorder();
// 設置輸出文件
mMediaRecorder.setOutputFile(file.getAbsolutePath());
// 設置MediaRecorder的音頻源為麥克風
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 設置音頻格式
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
// 設置音頻編碼
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// 準備錄音
mMediaRecorder.prepare();
// 開始
mMediaRecorder.start();
// 準備結(jié)束
isPrepare = true;
if (mAudioStateListener != null) {
mAudioStateListener.wellPrepared();
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 隨機生成文件的名稱
*/
private String generateFileName() {
return UUID.randomUUID().toString() + ".amr";
}
public int getVoiceLevel(int maxlevel) {
if (isPrepare) {
try {
return maxlevel * mMediaRecorder.getMaxAmplitude() / 32768 + 1;
} catch (Exception e) {
}
}
return 1;
}
/**
* 釋放資源
*/
public void release() {
if (mMediaRecorder != null) {
mMediaRecorder.reset();
mMediaRecorder = null;
}
}
/**
* 取消錄音
*/
public void cancel() {
release();
if (mCurrentFilePath != null) {
File file = new File(mCurrentFilePath);
file.delete();
mCurrentFilePath = null;
}
}
public String getCurrentFilePath() {
return mCurrentFilePath;
}
}
下面是MediaManager渡紫,播放,暫停錄音
public class MediaManager {
private static MediaPlayer mMediaPlayer;
private static boolean isPause;
private String currentFilePath;
private AudioManager.AudioStateListener onAudioStateListener;
static Context context;
public MediaManager(Context context) {
this.context = context;
}
/**
* 播放音樂
*
* @param filePath
* @param onCompletionListener
*/
public static void playSound(String filePath, MediaPlayer.OnCompletionListener onCompletionListener) {
if (mMediaPlayer == null) {
mMediaPlayer = new MediaPlayer();
// mMediaPlayer = MediaPlayer.create(context, Uri.fromFile(new File(filePath)));
//設置一個error監(jiān)聽器
mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
mMediaPlayer.reset();
return false;
}
});
} else {
mMediaPlayer.reset();
}
try {
mMediaPlayer.setAudioStreamType(android.media.AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnCompletionListener(onCompletionListener);
mMediaPlayer.setDataSource(filePath);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
}
}
/**
* 暫停播放
*/
public static void pause() {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
//正在播放的時候
mMediaPlayer.pause();
isPause = true;
}
}
public static boolean isPlaying() {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
return true;
} else {
return false;
}
}
/**
* 當前是isPause狀態(tài)
*/
public static void resume() {
if (mMediaPlayer != null && isPause) {
mMediaPlayer.start();
isPause = false;
}
}
/**
* 釋放資源
*/
public static void release() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
}
至此考赛,其整體的代碼已經(jīng)編寫完成了惕澎,在需要錄音的地方調(diào)用:
audioRecorderButton.setAudioFinishRecorderListener(new AudioRecorderButton.AudioFinishRecorderListener() {
@Override
public void onFinish(float seconds, String filePath) {
//seconds 錄音時長,filePath 錄音文件的位置
//錄音完成
upLoadVoice(filePath, (int) seconds);
}
});