Code for recording video on Android

im new for android development.

I had a requirement to record video on an Android device. When I searched for it, I did not find any suitable code that worked. even Android developers do not provide clear code.

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

Thank..

+3
source share
2 answers

Place the button in the xml file where you want to open the camera for recording video.

here I put the video encoding code.

this will create a folder on your SD card / or if no SD card is inserted, it will use inbult sytem memory. hope you know. I do not need to explain much about this.

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

- ,


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

, .

​​.

intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 30);

30 ,, , unlimietd time.. .. ok

+2

Media Recorder, : Android?

0

Source: https://habr.com/ru/post/1756323/


All Articles