// 录音完毕加载 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); }
// 取消录音,清除计时 private void cancelRecord() { // 可能在消息队列中还存在HandlerMessage,移除剩余消息 mVolumeHandler.removeMessages(56, null); mVolumeHandler.removeMessages(57, null); mVolumeHandler.removeMessages(58, null); mVolumeHandler.removeMessages(59, null); mTimeUp = false; cancelTimer(); stopRecording(); if (recordIndicator != null) recordIndicator.dismiss(); if (myRecAudioFile != null) myRecAudioFile.delete(); }