@SuppressLint("NewApi")
  private boolean initRecorder() {
    if (!EaseCommonUtils.isExitsSdcard()) {
      showNoSDCardDialog();
      return false;
    }

    if (mCamera == null) {
      if (!initCamera()) {
        showFailDialog();
        return false;
      }
    }
    mVideoView.setVisibility(View.VISIBLE);
    // TODO init button
    mCamera.stopPreview();
    mediaRecorder = new MediaRecorder();
    mCamera.unlock();
    mediaRecorder.setCamera(mCamera);
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    // 设置录制视频源为Camera(相机)
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    if (frontCamera == 1) {
      mediaRecorder.setOrientationHint(270);
    } else {
      mediaRecorder.setOrientationHint(90);
    }
    // 设置录制完成后视频的封装格式THREE_GPP为3gp.MPEG_4为mp4
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    // 设置录制的视频编码h263 h264
    mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    // 设置视频录制的分辨率。必须放在设置编码和格式的后面,否则报错
    mediaRecorder.setVideoSize(previewWidth, previewHeight);
    // 设置视频的比特率
    mediaRecorder.setVideoEncodingBitRate(384 * 1024);
    // // 设置录制的视频帧率。必须放在设置编码和格式的后面,否则报错
    if (defaultVideoFrameRate != -1) {
      mediaRecorder.setVideoFrameRate(defaultVideoFrameRate);
    }
    // 设置视频文件输出的路径
    localPath = PathUtil.getInstance().getVideoPath() + "/" + System.currentTimeMillis() + ".mp4";
    mediaRecorder.setOutputFile(localPath);
    mediaRecorder.setMaxDuration(30000);
    mediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
    try {
      mediaRecorder.prepare();
    } catch (IllegalStateException e) {
      e.printStackTrace();
      return false;
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }
Ejemplo n.º 2
0
  /** 照相获取图片 */
  protected void selectPicFromCamera() {
    if (!EaseCommonUtils.isExitsSdcard()) {
      Toast.makeText(getActivity(), R.string.sd_card_does_not_exist, 0).show();
      return;
    }

    cameraFile =
        new File(
            PathUtil.getInstance().getImagePath(),
            EMClient.getInstance().getCurrentUser() + System.currentTimeMillis() + ".jpg");
    cameraFile.getParentFile().mkdirs();
    startActivityForResult(
        new Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            .putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile)),
        REQUEST_CODE_CAMERA);
  }