如何为从库中选择的视频设置持续时间限制
Posted
技术标签:
【中文标题】如何为从库中选择的视频设置持续时间限制【英文标题】: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 分钟的时长限制?
【问题讨论】:
【参考方案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();
);
【讨论】:
Raj Suvariya @raj-suvariya 试试这个以上是关于如何为从库中选择的视频设置持续时间限制的主要内容,如果未能解决你的问题,请参考以下文章
从库中选择视频时未调用 uiimagepickercontroller didfinishpickingmediawithinfo