manidhar mulaparthi
manidhar mulaparthi

Reputation: 1082

code for video recording on android

im new on android development.

I had a requirement of recording video on an android device.. when i searched for it, i didn't find any proper code that is working. even android developers doesn't provide clear code..

Please, if anyone has links or code.. share with me..

Thank you..

Upvotes: 3

Views: 6706

Answers (2)

Bala Prasanna
Bala Prasanna

Reputation: 170

Put a button in xml file, where you want open the camera for video recording.

here by i put the video recoding code.

this will create a folder in your sdcard/ or if no sdcard inserted it will use sytem inbult memory. hope u ll know..i dont need to explain a lot on this.

then map the below on click listener to the button you designed in your xml


    Button recordButton = 
            (Button) findViewById(R.id.CaptureVid);
    recordButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

    String timestamp="1";
    String  timestamp = new SimpleDateFormat("MM-dd-yyyy_HH-mm-ss aa").format(Calendar.getInstance().getTime());
    File filepath = Environment.getExternalStorageDirectory();
    File dir = new File(filepath.getAbsolutePath()+ "/samplevideofolder/");
    dir.mkdirs();
    File mediaFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/samplevideofolder/Video_"+timestamp+".avi");  
    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    Uri fileUri = Uri.fromFile(mediaFile);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 30); 
    startActivityForResult(intent, VIDEO_CAPTURE);
        }
    });

put this code somewhere below , as function inside the main class


protected void onActivityResult(int requestCode, int resultCode, Intent data) {


    if (requestCode == VIDEO_CAPTURE) {
      if (resultCode == RESULT_OK) {



         Toast.makeText(this, "Video saved to:\n" +data.getData(), Toast.LENGTH_LONG).show();



      } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Video recording cancelled.",  Toast.LENGTH_LONG).show();
      } else {
         //Toast.makeText(this, "Failed to record video",                        Toast.LENGTH_LONG).show();
        }
    }
}

thats it, you are done with video recording..

in the above code you can find a line like this.

intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 30);

this will automatically stops the video after 30 seconds,,, if you want to record the video for more than that, unlimietd time.. just comment that line.. ok

Upvotes: 2

Robert Massaioli
Robert Massaioli

Reputation: 13477

You do it using the Media Recorder class as this explains: How can I capture a video recording on Android?

Upvotes: 0

Related Questions