// 录音完毕加载 ListView item private void finishRecord() { cancelTimer(); stopRecording(); if (recordIndicator != null) recordIndicator.dismiss(); long intervalTime = System.currentTimeMillis() - startTime; if (intervalTime < MIN_INTERVAL_TIME) { MUIToast.toast(getContext(), R.string.time_too_short_toast); myRecAudioFile.delete(); return; } else { if (myRecAudioFile != null && myRecAudioFile.exists()) { MediaPlayer mp = MediaPlayer.create(mContext, Uri.parse(myRecAudioFile.getAbsolutePath())); // 某些手机会限制录音,如果用户拒接使用录音,则需判断mp是否存在 if (mp != null) { int duration = mp.getDuration() / 1000; // 即为时长 是s if (duration < 1) duration = 1; else if (duration > 60) duration = 60; mHandler .obtainMessage( Api.UPLOAD_VOICE_CALLBACK, duration, 0, myRecAudioFile.getAbsolutePath()) .sendToTarget(); } else { MUIToast.toast(mContext, R.string.record_voice_permission_request); } } } }
private void initDialogAndStartRecord() { // 存放录音文件目录 File destDir = mContext.getFilesDir(); // 录音文件的命名格式 myRecAudioFile = new File( destDir, new DateFormat().format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".amr"); if (myRecAudioFile == null) { cancelTimer(); stopRecording(); MUIToast.toast(mContext, R.string.create_file_failed); } Log.i("FileCreate", "Create file success file path: " + myRecAudioFile.getAbsolutePath()); vibrate(); recordIndicator = new Dialog(getContext(), R.style.record_voice_dialog); View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_record_voice, null); // Window dialogWindow = recordIndicator.getWindow(); // dialogWindow.setLayout((int) (0.8 * mWidth), // WindowManager.LayoutParams.WRAP_CONTENT); mVolumeIv = (ImageView) view.findViewById(R.id.dialog_voice_icon); mRecordHintTv = (TextView) view.findViewById(R.id.dialog_voice_message); mRecordHintTv.setText(mContext.getString(R.string.move_to_cancel_hint)); startRecording(); recordIndicator.show(); recordIndicator.setContentView(view); }
@Override public boolean onTouchEvent(MotionEvent event) { this.setPressed(true); int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: // mChatView.startRecordAnim(); this.setText(mContext.getString(R.string.send_voice_hint)); mIsPressed = true; time1 = System.currentTimeMillis(); mTouchY1 = event.getY(); // 检查sd卡是否存在 sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); if (sdCardExist) { if (isTimerCanceled) { timer = createTimer(); } timer.schedule( new TimerTask() { @Override public void run() { android.os.Message msg = myHandler.obtainMessage(); msg.what = START_RECORD; msg.sendToTarget(); } }, 500); } else { MUIToast.toast(this.getContext(), R.string.sdcard_not_exist_toast); this.setPressed(false); this.setText(mContext.getString(R.string.record_voice_hint)); mIsPressed = false; return false; } break; case MotionEvent.ACTION_UP: // mChatView.stopRecordAnim(); this.setText(mContext.getString(R.string.record_voice_hint)); mIsPressed = false; this.setPressed(false); mTouchY2 = event.getY(); time2 = System.currentTimeMillis(); if (time2 - time1 < 500) { cancelTimer(); return true; } else if (time2 - time1 < 1000) { cancelRecord(); } else if (mTouchY1 - mTouchY2 > MIN_CANCEL_DISTANCE) { cancelRecord(); } else if (time2 - time1 < 60000) finishRecord(); break; case MotionEvent.ACTION_MOVE: mTouchY = event.getY(); // 手指上滑到超出限定后,显示松开取消发送提示 if (mTouchY1 - mTouchY > MIN_CANCEL_DISTANCE) { this.setText(mContext.getString(R.string.cancel_record_voice_hint)); mVolumeHandler.sendEmptyMessage(CANCEL_RECORD); // mChatView.stopRecordAnim(); // mChatView.setWaveHeight(5); if (mThread != null) mThread.exit(); mThread = null; } else { this.setText(mContext.getString(R.string.send_voice_hint)); if (mThread == null) { mThread = new ObtainDecibelThread(); mThread.start(); } } break; case MotionEvent.ACTION_CANCEL: // 当手指移动到view外面,会cancel this.setText(mContext.getString(R.string.record_voice_hint)); // mChatView.stopRecordAnim(); cancelRecord(); break; } return true; }