我想问一下为什么我正常录音结束以后正在录音的dialog没有dimiss掉一直在显示呢??
package com.example.administrator.testdemo.com.example.administator.testdemo.view;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import com.example.administrator.testdemo.R;
/**
* Created by Administrator on 2016/8/23.
*/
public class audioRecorderButton extends Button implements AudioManager.AudioStateListener {
private static final int DISTANCE_Y_CANCEL = 50;
private static final int STATE_NORMAL = 1;//默认状态
private static final int STATE_RECORDING = 2;//录音状态
private static final int STATE_WANT_TO_CANCEL = 3;//取消状态
private static int mCurState = STATE_NORMAL;//当前记录状态
private static boolean isRecording = false;//已经开始录音
private dialogManager mDialogManager;
private AudioManager mAudioManager;
private float mTime;
private boolean mReady;//是否触发longclick
public audioRecorderButton(Context context, AttributeSet attrs) {
super(context, attrs);
String dir = Environment.getExternalStorageDirectory() + "/abc";
mAudioManager = AudioManager.getInstance(dir);
mAudioManager.setOnAudioStateListener(this);
mDialogManager = new dialogManager(getContext());
setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mAudioManager.Preparing();
mReady = true;
return false;
}
});
}
//录音完成后的回调
private interface AudioFinishRecorderListener {
void onFinish(float second, String filePath);
}
public AudioFinishRecorderListener mListener;
public void setAudioFinishRecorderListener(AudioFinishRecorderListener listener) {
mListener = listener;
}
private Runnable mGetVoiceLevelRunnable = new Runnable() {
@Override
public void run() {
while (isRecording) {
try {
Thread.sleep(100);
mTime += 0.1f;
mHandle.sendEmptyMessage(MSG_VOICE_CHANGE);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
private static final int MSG_DIALOG_PREPARE = 0x110;
private static final int MSG_VOICE_CHANGE = 0x111;
private static final int MSG_DIALOG_DISMISS = 0x112;
private Handler mHandle = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case MSG_DIALOG_PREPARE:
isRecording = true;
mDialogManager.showRecordingDialog();
new Thread(mGetVoiceLevelRunnable).start();
break;
case MSG_VOICE_CHANGE:
mDialogManager.updateVoiceLevel(mAudioManager.getVoiceLevel(7));
break;
case MSG_DIALOG_DISMISS:
mDialogManager.diMissDialog();
break;
}
}
};
public void wellPrepare() {
mHandle.sendEmptyMessage(MSG_DIALOG_PREPARE);
}
public audioRecorderButton(Context context) {
this(context, null);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
int x = (int) event.getX();
int y = (int) event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
changeState(STATE_RECORDING);
break;
case MotionEvent.ACTION_MOVE:
//根据x,y的值,确定是否取消
if (isRecording) {
if (wantToCancel(x, y)) {
changeState(STATE_WANT_TO_CANCEL);
} else {
changeState(STATE_NORMAL);
}
}
break;
case MotionEvent.ACTION_UP:
if (!mReady){
reset();
return super.onTouchEvent(event);
}
else if ( !isRecording||mTime < 0.6f) {
mDialogManager.tooShort();
mHandle.sendEmptyMessageDelayed(MSG_DIALOG_DISMISS, 1300);//发送一个延迟,延迟时间为关闭dialog
mAudioManager.Cancel();
}
if (mCurState==STATE_RECORDING) //正常录制结束
{
mDialogManager.diMissDialog();
mAudioManager.Release();
if (mListener!=null){
mListener.onFinish(mTime,mAudioManager.getCurrentFilePath());
}
}
else if (mCurState == STATE_WANT_TO_CANCEL) {
mDialogManager.diMissDialog();
mAudioManager.Cancel();
}
reset();
break;
}
return super.onTouchEvent(event);
}
//恢复状态及标志位
private void reset() {
isRecording = false;
mReady = false;
mTime = 0f;
changeState(STATE_NORMAL);
}
private boolean wantToCancel(int x, int y) {
if (x < 0 || x > getWidth()) {
return true;
}
if (y < -DISTANCE_Y_CANCEL || y > getHeight() + DISTANCE_Y_CANCEL) {
return true;
}
return false;
}
private void changeState(int state) {
if (mCurState != state) {
mCurState = state;
switch (state) {
case STATE_NORMAL:
setBackgroundResource(R.drawable.btn_recorder_normal);
setText(R.string.str_recorder_normal);
break;
case STATE_WANT_TO_CANCEL:
setBackgroundResource(R.drawable.btn_recording);
setText(R.string.str_recorder_want_cancel);
mDialogManager.wanToCancel();
break;
case STATE_RECORDING:
setBackgroundResource(R.drawable.btn_recording);
setText(R.string.str_recorder_recording);
mDialogManager.recording();
break;
}
}
}
}