是否可以使用 Intent 设置 Android 录音允许的最长时间?

发布于 2024-12-12 11:24:14 字数 199 浏览 0 评论 0原文

我正在使用 android.provider.MediaStore.ACTION_VIDEO_CAPTURE。我想知道是否有办法更改每次录制允许的最长时间。我尝试添加 Intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,60000);//最长 60 秒 但它继续记录这一点。提前致谢。

I am using the android.provider.MediaStore.ACTION_VIDEO_CAPTURE. I was wondering if there is a way to change the maximum time allowed per recording. I TRIED ADDING
Intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,60000);//max of 60 seconds
but it continues recording pass that. Thanks in advance.

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

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

发布评论

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

评论(6

撧情箌佬 2024-12-19 11:24:15
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra("android.intent.extra.durationLimit", 30000);
intent.putExtra("EXTRA_VIDEO_QUALITY", 0);
startActivityForResult(intent, ActivityRequests.REQUEST_TAKE_VIDEO);

此代码在 API 2.2 上运行良好,但持续时间限制在 API 2.1 上不起作用

android.intent.extra.durationLimit 是在 API Level 8 中引入的,因此不可用不幸的是,在闪电泡芙和更早的时候。一些设备制造商可能有一种专有的方法来设置旧设备上的最大持续时间,这解释了为什么您在某些 Froyo 之前的应用程序上看到了这种方法。

Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra("android.intent.extra.durationLimit", 30000);
intent.putExtra("EXTRA_VIDEO_QUALITY", 0);
startActivityForResult(intent, ActivityRequests.REQUEST_TAKE_VIDEO);

This code works well on API 2.2, but the duration limit does not work on API 2.1

android.intent.extra.durationLimit was introduced in API Level 8, so it's not available in Eclair and earlier, unfortunately. Some device manufacturers may have a proprietary way to set the maximum duration on older devices, which explain why you have seen this working on some pre-Froyo applications.

或十年 2024-12-19 11:24:15

用 30 秒的时间,尝试这个代码。

intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 30);

For 30 seconds time, try this code.

intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 30);
请持续率性 2024-12-19 11:24:15

以下是 Kotlin 更新,用于启动 ACTION_VIDEO_CAPTURE Intent,并将 EXTRA_DURATION_LIMIT 设置为 60 秒。至于 putExtra(MediaStore.EXTRA_DURATION_LIMIT, 60) 以秒为单位作为持续时间限制的值。

val recordVideoIntent = Intent(MediaStore.ACTION_VIDEO_CAPTURE)

recordVideoIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 60)
startActivityForResult(recordVideoIntent, INTENT_VIDEO_RECORD_REQUEST)

Here's a Kotlin update to launch ACTION_VIDEO_CAPTURE Intent with EXTRA_DURATION_LIMIT set to 60 seconds. As for putExtra(MediaStore.EXTRA_DURATION_LIMIT, 60) is taking in seconds as the value for the duration limit.

val recordVideoIntent = Intent(MediaStore.ACTION_VIDEO_CAPTURE)

recordVideoIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 60)
startActivityForResult(recordVideoIntent, INTENT_VIDEO_RECORD_REQUEST)
∞梦里开花 2024-12-19 11:24:15

使用这个,这里 60 是第二个
代码:

intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 60);

Use this,here 60 is second
Code:

intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 60);
千里故人稀 2024-12-19 11:24:15

使用 MediaRecorder

 /**
     * Starts a new recording.
     */
    public void start() throws IOException {

    recorder = new MediaRecorder();

    String state = android.os.Environment.getExternalStorageState();

    if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
        throw new IOException("SD Card is not mounted.  It is " + state
            + ".");
    }

    // make sure the directory we plan to store the recording in exists
    File directory = new File(path).getParentFile();
    System.out.println("start() directory >  " + directory);
    if (!directory.exists() && !directory.mkdirs()) {
        throw new IOException("Path to file could not be created.");
    }



    recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // Sets the
    // audio source
    // to be used
    // for recording



    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); // Sets
    // the
    // format
    // of
    // the
    // output
    // file
    // produced
    // during
    // recording.
    // 5 Minutes = 300000 Milliseconds

    recorder.setMaxDuration(300000); // Sets the maximum duration (in ms) of
    // the recording session



    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); // Sets the
    // audio
    // encoder
    // to be
    // used for
    // recording.

    recorder.setOutputFile(path); // Sets the path of the output file to be
    // produced.
    recorder.prepare(); // Prepares the recorder to begin capturing and
    // encoding data.
    recorder.start(); // Recording is now started

}

Use MediaRecorder

 /**
     * Starts a new recording.
     */
    public void start() throws IOException {

    recorder = new MediaRecorder();

    String state = android.os.Environment.getExternalStorageState();

    if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
        throw new IOException("SD Card is not mounted.  It is " + state
            + ".");
    }

    // make sure the directory we plan to store the recording in exists
    File directory = new File(path).getParentFile();
    System.out.println("start() directory >  " + directory);
    if (!directory.exists() && !directory.mkdirs()) {
        throw new IOException("Path to file could not be created.");
    }



    recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // Sets the
    // audio source
    // to be used
    // for recording



    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); // Sets
    // the
    // format
    // of
    // the
    // output
    // file
    // produced
    // during
    // recording.
    // 5 Minutes = 300000 Milliseconds

    recorder.setMaxDuration(300000); // Sets the maximum duration (in ms) of
    // the recording session



    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); // Sets the
    // audio
    // encoder
    // to be
    // used for
    // recording.

    recorder.setOutputFile(path); // Sets the path of the output file to be
    // produced.
    recorder.prepare(); // Prepares the recorder to begin capturing and
    // encoding data.
    recorder.start(); // Recording is now started

}

上课铃就是安魂曲 2024-12-19 11:24:14

实际上,MediaStore.EXTRA_DURATION_LIMIT 提供的时间单位是,而不是毫秒!
所以你只需要把你的值从 60000 改为 60 ;)
Android 文档

Actually, MediaStore.EXTRA_DURATION_LIMIT provide time in seconds, NOT in miliseconds!
So you just need to change your value from 60000 to 60 ;)
Android Documentation

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