【问题标题】:How to set a duration limit to videos chosen from library如何为从库中选择的视频设置持续时间限制
【发布时间】:2015-12-04 04:47:16
【问题描述】:

我正在尝试限制将上传到服务器的视频的持续时间。

我使用此代码让用户从图库中选择一个视频:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setType("video/*");
List<ResolveInfo> list = getPackageManager().queryIntentActivities(
intent, PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() <= 0) {
Log.d(TAG, "no video picker intent on this hardware");
return;
}
startActivityForResult(intent, GALLERY_RETURN);

如何为从图库中选择的视频设置 1 分钟的时长限制?

【问题讨论】:

    标签: android video android-sdcard


    【解决方案1】:
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_PICK);
    intent.setType("video/*");
    List<ResolveInfo> list = getPackageManager().queryIntentActivities(
    intent, PackageManager.MATCH_DEFAULT_ONLY);
    if (list.size() <= 0) {
    Log.d(TAG, "no video picker intent on this hardware");
    return;
    }
    long maxVideoSize = 24 * 1024 * 1024; // 10 MB
    intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, maxVideoSize);
    
    startActivityForResult(intent, GALLERY_RETURN);
    

    【讨论】:

    • 实际上我正在尝试将视频时长限制为不超过 1 分钟......但你给了我一个想法谢谢
    • 你也可以使用 MediaStore.EXTRA_DURATION_LIMIT
    • 24 * 1024 * 1024 可能不是 10MB
    • @IbrahimMAATKI 你找到解决办法了吗?
    【解决方案2】:
    mediaRecorder = new MediaRecorder();
    
        mediaRecorder.setCamera(myCamera);
    
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        try {
            mediaRecorder.setProfile(CamcorderProfile
                    .get(CamcorderProfile.QUALITY_480P));
        } catch (Exception e) {
            mediaRecorder.setProfile(CamcorderProfile
                    .get(CamcorderProfile.QUALITY_LOW));
        }
    
        mediaRecorder
                .setOutputFile(Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                        + "/"
                        + Config.IMAGE_DIRECTORY_NAME
                        + "/TempVideo.mp4");
        mediaRecorder.setMaxDuration(60000); // Set max duration 30 sec max of
                                                // twitter.
        // mediaRecorder.setMaxFileSize(14340032); // Set max file size ~ 7M
    
        mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder()
                .getSurface());
        mediaRecorder.setOrientationHint(90);
        mediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
            public void onInfo(MediaRecorder mr, int what, int extra) {
                if ((what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED)
                        || (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED)) {
                    updateView();
                }
            }
        });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    相关资源
    最近更新 更多