MotionEvent.ACTION_UP分支未响应
package com.example.diego.talk;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
/**
* Created by diego on 2017/8/26.
*/
public class AudioRecorderButton extends Button implements AudioManager.AudioStateListener {
private static final int Y_CANCEL = 50;
private static final int STATE_NORMAL = 1;
private static final int STATE_RECORDING = 2;
private static final int STATE_WANT_CANCEL = 3;
private AudioManager audioManager;
private float mTime;
//是否触发longclick
private boolean mReady=false;
private boolean isRecording = false;
private int curState = STATE_NORMAL;
private DialogManager dialogManager;
public AudioRecorderButton(Context context) {
super(context, null);
}
public AudioRecorderButton(Context context, AttributeSet attrs) {
super(context, attrs);
dialogManager = new DialogManager(context);
//TODO 先判断是否插入了SD卡
String dir = Environment.getExternalStorageDirectory() + "/trekker_recorder_audios";
audioManager = AudioManager.getInstance(dir);
audioManager.setOnAudioStateListener(this);
setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
mReady = true;
audioManager.prepareAudio();
return false;
}
});
}
public interface AudioFinishRecorderListener{
void onFinish(float seconds,String filePath);
}
private 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;
mHander.sendEmptyMessage(MSG_VOICE_CHANGED);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
private static final int MSG_AUDIO_PREPARED = 0X110;
private static final int MSG_VOICE_CHANGED = 0X111;
private static final int MSG_AUDIO_DISMISS = 0X112;
private Handler mHander = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case MSG_AUDIO_PREPARED:
dialogManager.showRecordingDialog();
isRecording = true;
new Thread(mGetVoiceLevelRunnable).start();
break;
case MSG_VOICE_CHANGED:
dialogManager.updateVoiceLevel(audioManager.getVoiceLevel(7));
break;
case MSG_AUDIO_DISMISS:
break;
}
}
};
@Override
public void wellPrepare() {
mHander.sendEmptyMessage(MSG_AUDIO_PREPARED);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.e("onTouchEvent","onTouchEvent");
int action = event.getAction();
int x = (int) event.getX();
int y = (int) event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.e("down","down");
ChangeState(STATE_RECORDING);
wellPrepare();
break;
case MotionEvent.ACTION_MOVE:
Log.e("move","move");
if (isRecording) {
if (wantCancel(x, y)) {
ChangeState(STATE_WANT_CANCEL);
} else {
ChangeState(STATE_RECORDING);
}
}
break;
case MotionEvent.ACTION_UP:
Log.e("up","");
if (!mReady) {
reset();
return super.onTouchEvent(event);
} else if (!isRecording || mTime < 0.6) {
Log.e("tooshort","1");
dialogManager.tooShort();
audioManager.cancel();
mHander.sendEmptyMessageDelayed(MSG_AUDIO_DISMISS, 1300);
} else if (curState == STATE_RECORDING) {
Log.e("recording","1");
dialogManager.dismissDialog();
if(mListener!=null){
mListener.onFinish(mTime,audioManager.getCurrentPath());
}
audioManager.release();
} else if (curState == STATE_WANT_CANCEL) {
Log.e("wantcancel","1");
dialogManager.dismissDialog();
audioManager.cancel();
}
Log.e("reset","");
reset();
break;
}
return super.onTouchEvent(event);
}
private void reset() {
isRecording = false;
mReady = false;
mTime = 0;
ChangeState(STATE_NORMAL);
}
private boolean wantCancel(int x, int y) {
if (x < 0 || x > getWidth()) {
return true;
}
if (y < -Y_CANCEL || y > getWidth() + Y_CANCEL) {
return true;
}
return false;
}
private void ChangeState(int state) {
if (curState != state) {
switch (state) {
case STATE_NORMAL:
Log.i("start","1");
setBackgroundResource(R.drawable.btn_recorder_normal);
setText(R.string.please_press);
curState = STATE_NORMAL;
break;
case STATE_RECORDING:
setBackgroundResource(R.drawable.btn_recording);
setText(R.string.recording);
curState = STATE_RECORDING;
if (isRecording) {
dialogManager.recording();
} else {
isRecording = true;
}
break;
case STATE_WANT_CANCEL:
setBackgroundResource(R.drawable.btn_recording);
curState = STATE_WANT_CANCEL;
setText(R.string.want_cancel);
dialogManager.wantToCancel();
break;
}
}
}
}