monu
monu

Reputation: 35

Starting video capture on one click in android

I want to start camera and also to automatically start recording just by clicking an app in android. I have the code to start the camera but I do not know how to start auto capture of the video. Please help. the code I have for launching camera-

@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);
}

I found about view.performclick but do not know how to use for camera

Upvotes: 1

Views: 3703

Answers (2)

nkr
nkr

Reputation: 128

Use MediaRecorder for this purpose. Though it will require more work but will give you much more control. Follow this link http://android-er.blogspot.tw/2011/04/start-video-recording-using.html. Since you don't reuire any button click for recording, keep a delay before starting the camera. Do it like this

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

Upvotes: 1

Lucifer
Lucifer

Reputation: 29642

Ok, Make following, changes in your code.

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);
        }
   });
}

Upvotes: 1

Related Questions