录制 Demo

创建录制监听器

代码示例:

  1. SightCameraView.OnRecordListener cameraListener = new SightCameraView.OnRecordListener() {
  2. @Override
  3. public void onStart() {
  4. }
  5. @Override
  6. public void onError(APVideoRecordRsp rsp) {
  7. String memo = "相机错误";
  8. switch (rsp.mRspCode) {
  9. case APVideoRecordRsp.CODE_ERR_MIC_PERMISSION_DENIED:
  10. case APVideoRecordRsp.CODE_ERR_MIC_UNKNOWN_ERROR:
  11. case APVideoRecordRsp.CODE_ERR_MIC_INVALID_OPERATION:
  12. case APVideoRecordRsp.CODE_ERR_MIC_BAD_VALUE:
  13. case APVideoRecordRsp.CODE_ERR_MIC_INVALID_BUFFER_INDEX:
  14. case APVideoRecordRsp.CODE_ERR_MIC_STOP_FAILED:
  15. memo = getString(R.string.tips_mic_error);
  16. break;
  17. case APVideoRecordRsp.CODE_ERR_SDCARD_UNAVAILABLE:
  18. memo = getString(R.string.tips_sdcard_error);
  19. break;
  20. case APVideoRecordRsp.CODE_ERR_DISK_SPACE_NOT_ENOUGH:
  21. memo = getString(R.string.tips_sdcard_not_enough);
  22. break;
  23. case APVideoRecordRsp.CODE_ERR_CAMERA_OPEN:
  24. memo = getString(R.string.check_camera_permission);
  25. break;
  26. default:
  27. break;
  28. }
  29. if (isRecording()) {
  30. cancelRecord(false);
  31. }
  32. alert(null, memo, getString(R.string.confirm), new DialogInterface.OnClickListener() {
  33. @Override
  34. public void onClick(DialogInterface dialog, int which) {
  35. finish();
  36. }
  37. }, null, null, false, false);
  38. }
  39. @Override
  40. public void onFinish(APVideoRecordRsp rsp) {
  41. onRecordFinish(rsp);
  42. }
  43. @Override
  44. public void onCancel() {
  45. onRecordCanceled();
  46. }
  47. @Override
  48. public void onPrepared(APVideoRecordRsp rsp) {
  49. if (rsp == null || rsp.mCamera == null) {
  50. LogCatLog.e(TAG, "相机onPrepare错误,camera 为空");
  51. alert(null, getString(R.string.check_camera_permission), getString(R.string.confirm),
  52. new DialogInterface.OnClickListener() {
  53. @Override
  54. public void onClick(DialogInterface dialog, int which) {
  55. finish();
  56. }
  57. }, null, null);
  58. } else {
  59. camera = rsp.mCamera;
  60. onCameraPrepared();
  61. handler.post(new Runnable() {
  62. @Override
  63. public void run() {
  64. ivPrepare.setVisibility(View.GONE);
  65. }
  66. });
  67. }
  68. }
  69. @Override
  70. public void onInfo(int i, Bundle bundle) {
  71. }
  72. };

创建录制控件

创建录制控件会耗费一定时间,请在 Activity 的 onWindowFocusChanged 中创建。创建控件的正确方式是使用 MultimediaVideoService,并将控件的初始化放在onWindowFocusChanged 后面,不阻塞主线程加载 Activity。

  1. @Override
  2. public void onWindowFocusChanged(boolean hasFocus) {
  3. super.onWindowFocusChanged(hasFocus);
  4. if (!hasFocus) {
  5. return;
  6. }
  7. //将 CameraView 的初始化放在 onWindowFocusChanged 后面,不阻塞主线程加载Activity
  8. if (cameraView != null)
  9. return;
  10. videoService = findServiceByInterface(MultimediaVideoService.class.getName());
  11. cameraView = videoService.createCameraView(this, this, null);//创建控件的正确方式是使用 MultimediaVideoService
  12. cameraView.setOnRecordListener(cameraListener);
  13. FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
  14. ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
  15. cameraView.setLayoutParams(layoutParams);
  16. ((FrameLayout) findViewById(R.id.cameraViewContainer)).addView(cameraView);
  17. }

创建 SightCameraView 控件

创建一个简单完整的 SightCameraView 控件的代码示例如下:

  1. /**
  2. * Alipay.com Inc.
  3. * Copyright (c) 2004-2016 All Rights Reserved.
  4. */
  5. package com.alipay.android.phone.nfd.wifisdk.django.demo;
  6. import com.alipay.android.phone.mobilecommon.multimedia.api.MultimediaVideoService;
  7. import com.alipay.android.phone.mobilecommon.multimedia.video.data.CameraParams;
  8. import com.alipay.android.phone.mobilecommon.multimedia.widget.SightCameraView;
  9. import com.alipay.mobile.framework.app.ui.BaseActivity;
  10. import android.os.Bundle;
  11. import android.view.ViewGroup;
  12. import android.widget.FrameLayout;
  13. /**
  14. * @author longfei.ylf
  15. * @version $$Id: CameraViewActivity.java, v 0.1 2016/7/28 17:08 longfei.ylf Exp $$
  16. */
  17. public class CameraViewActivity extends BaseActivity {
  18. private MultimediaVideoService mVideoService;
  19. private FrameLayout mFrameLayout;
  20. SightCameraView mCameraView;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. ViewGroup.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT
  25. , FrameLayout.LayoutParams.MATCH_PARENT);
  26. mFrameLayout = new FrameLayout(this);
  27. setContentView(mFrameLayout);
  28. }
  29. @Override
  30. protected void onResume() {
  31. super.onResume();
  32. if (mCameraView != null) {
  33. //mCameraView.onResume();
  34. }
  35. }
  36. @Override
  37. protected void onStop() {
  38. super.onStop();
  39. if (mCameraView != null) {
  40. //mCameraView.onStop();
  41. }
  42. }
  43. public void onWindowFocusChanged(boolean hasFocus) {
  44. if (hasFocus) {
  45. runOnUiThread(new Runnable() {
  46. @Override
  47. public void run() {
  48. if (mCameraView != null)
  49. return;
  50. mVideoService = mMicroApplicationContext.getExtServiceByInterface(
  51. MultimediaVideoService.class.getName());
  52. ViewGroup.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT
  53. , FrameLayout.LayoutParams.MATCH_PARENT);
  54. CameraParams params = new CameraParams();
  55. params.recordType = CameraParams.REC_TYPE_DEFAULT;
  56. mCameraView = mVideoService.createCameraView(CameraViewActivity.this);
  57. mFrameLayout.addView(mCameraView, layoutParams);
  58. }
  59. });
  60. }
  61. }
  62. }