1

我想启动相机并通过单击android中的应用程序来自动开始录制。我有启动相机的代码,但我不知道如何开始自动捕捉视频。请帮忙。我用于启动相机的代码-

@Override 
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState); 
   setContentView(R.layout.activity_c1_main);
   Intent intent = new Intent("android.media.action.VIDEO_CAPTURE");
   StartActivityForResult(intent,CAPTURE_VIDEO_ACTIVITY);
}

我发现了 view.performclick 但不知道如何用于相机

4

2 回答 2

1

好的,在您的代码中进行以下更改。

Button play;

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

   play = findViewById ( R.id.btnPlay );         // assuming you have this button in your .xml file.

   play.setOnClickListener ( new OnClickListener()
   {
        @Override
        public void onClick ( View view )
        {
           Intent intent = new Intent("android.media.action.VIDEO_CAPTURE");
           StartActivityForResult(intent,CAPTURE_VIDEO_ACTIVITY);
        }
   });
}
于 2013-05-13T10:04:40.450 回答
1

为此目的使用 MediaRecorder。虽然它需要更多的工作,但会给你更多的控制。按照此链接http://android-er.blogspot.tw/2011/04/start-video-recording-using.html。由于您不需要单击任何按钮进行录制,因此请在启动相机之前保持延迟。像这样做

myButton.setPressed(true); //you won't click the button
myButton.invalidate();
myButton.postDelayed(new Runnable() {  
    public void run() {      
         myButton.setPressed(false); 
         myButton.invalidate();
         releaseCamera();   //release camera from preview before MediaRecorder starts
        if(!prepareMediaRecorder()){
            Toast.makeText(AndroidVideoCapture.this,"could not prepare MediaRecorder",Toast.LENGTH_LONG).show();
            finish();
        }
        mediaRecorder.start();
    }
},5000); //causes delay of 5 seconds befor recording starts
于 2013-05-29T05:25:06.760 回答