自定义视频录像机插件不起作用

时间:2018-07-11 16:20:42

标签: android android-video-record

我现在正在使用此插件https://github.com/natario1/CameraView#cameravideocodec开发一个简单的Android自定义录像机。它很好地显示了相机预览。但是问题是onVideoTaken回调没有被调用。

这是我对自定义录像机的活动:

public class VideoRecorderActivity extends AppCompatActivity
{

   private CameraView viewVideoPreview;
   private AppCompatButton btnCaptureVideo;
   private AppCompatButton btnSwitchToPhoto;
   private boolean capturingVideo;
   private int eventId;
   private String eventCode;

   @Override protected void onCreate(@Nullable Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_video_recorder);

      this.initialize();
      this.initializeViews();
      this.setUpViews();
   }

   private void initialize()
   {
      this.capturingVideo = false;
   }

   private void initializeViews()
   {
      this.viewVideoPreview = (CameraView)findViewById(R.id.video_view);
      this.btnCaptureVideo = (AppCompatButton)findViewById(R.id.btn_capture_video);
      this.btnSwitchToPhoto = (AppCompatButton)findViewById(R.id.btn_switch_to_phto);
   }

   private void setUpViews()
   {
      viewVideoPreview.addCameraListener(new CameraListener()
      {
         @Override public void onVideoTaken(File video)
         {
            //super.onVideoTaken(video);
            Toast.makeText(getApplicationContext(), "Event trigged", Toast.LENGTH_SHORT).show();
            //save video
            //This callback is not called.
         }
      });

      viewVideoPreview.setVideoQuality(VideoQuality.HIGHEST);

       btnCaptureVideo.setOnClickListener(new View.OnClickListener()
       {
          @Override public void onClick(View v)
          {
             if(capturingVideo)
             {
                //stop
                viewVideoPreview.stopCapturingVideo();
                btnCaptureVideo.setText("Start capturing video");
                capturingVideo = false;
                //Toast.makeText(getApplicationContext(), "Video saved..", Toast.LENGTH_SHORT).show();
             }
             else
             {
                startVideoCapturing();
                btnCaptureVideo.setText("Stop recording");
                capturingVideo = true;
             }
          }
       });

       btnSwitchToPhoto.setOnClickListener(new View.OnClickListener()
       {
          @Override public void onClick(View v)
          {
             startCameraActivity();
          }
       });
   }

   private void startCameraActivity()
   {
      Intent intent = new Intent(this, CamActivity.class);
      intent.putExtra(CamActivity.FIELD_EVENT_CODE, this.eventCode);
      intent.putExtra(CamActivity.FIELD_EVENT_ID, this.eventId);
      startActivity(intent);
   }

   private void startVideoCapturing()
   {
      String root = Environment.getExternalStorageDirectory().toString();
      File dir = new File(root + "/memento_videos");
      if(!dir.exists())
      {
         dir.mkdirs();

      }
      String fileName = "memento_" + String.valueOf(System.currentTimeMillis()) + ".mp4";
      File file = new File(dir, fileName);
      viewVideoPreview.startCapturingVideo(file);
   }


   @Override
   protected void onResume() {
      super.onResume();
      viewVideoPreview.start();
   }

   @Override
   protected void onPause() {
      super.onPause();
      viewVideoPreview.stop();
   }

   @Override
   protected void onDestroy() {
      super.onDestroy();
      viewVideoPreview.destroy();
   }
}

问题是未调用onVideoTaken回调。

这是我的xml布局文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">

   <com.otaliastudios.cameraview.CameraView
      android:id="@+id/video_view"
      android:keepScreenOn="true"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />


   <LinearLayout
      android:gravity="center"
      android:layout_gravity="center"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
      <android.support.v7.widget.AppCompatButton
         android:padding="5dp"
         android:textColor="@android:color/white"
         android:background="@color/colorAccent"
         android:id="@+id/btn_capture_video"
         android:text="Start recording video"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
      <android.support.v7.widget.AppCompatButton
         android:padding="5dp"
         android:textColor="@android:color/white"
         android:background="@color/colorLight"
         android:id="@+id/btn_switch_to_phto"
         android:text="Photo"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
   </LinearLayout>
</LinearLayout>

我的代码中缺少什么?为什么它不起作用,并且插件中有错误?

0 个答案:

没有答案