Android 上的视频录制代码

发布于 2024-09-11 02:30:12 字数 140 浏览 4 评论 0原文

我是 Android 开发新手。

我需要在 Android 设备上录制视频。当我搜索它时,我没有找到任何有效的正确代码。即使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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

§普罗旺斯的薰衣草 2024-09-18 02:30:12

在 xml 文件中放置一个按钮,在其中要打开相机进行视频录制。

我在这里放置了视频重新编码代码。

这将在您的 SD 卡/中创建一个文件夹,或者如果未插入 SD 卡,它将使用系统内存。希望你知道……我不需要对此做太多解释。

然后将下面的点击侦听器映射到您在 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 秒后自动停止视频,,,
如果你想录制超过这个时间的视频,无限时间..只需评论该行..好的

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

想你只要分分秒秒 2024-09-18 02:30:12

您可以使用 Media Recorder 类来执行此操作,如下所示:如何捕获视频录制安卓?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文