从图库中选择视频:仅限制不超过30秒的视频 [英] Video selection from gallery: Limit only videos up to 30 seconds should be selected

查看:112
本文介绍了从图库中选择视频:仅限制不超过30秒的视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何选择最长30秒的视频控制方式,否则显示Toast / Popup。
我可以在 onActivityResult 中获取视频的路径,并且可以运行视频,但无法获取时长。任何建议,以下是我的代码:

How can I control videos up to 30 seconds should be selected else show Toast/Popup. I can get video's path in onActivityResult and can run video but can't get duration. Any suggestion, below is my code:

case Utils.REQUEST_CODE_GALLERY_VIDEO:

        if (resultCode == Activity.RESULT_OK) {
            Uri selectedVieo = data.getData();
            String[] filePathColumn = { MediaStore.Video.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedVieo,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();

            try {
                if (filePath != null) {
                    runVideo(filePath);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


推荐答案

尝试

case Utils.REQUEST_CODE_GALLERY_VIDEO:

        if (resultCode == Activity.RESULT_OK) {
            Uri selectedVieo = data.getData();
            String[] filePathColumn = { MediaStore.Video.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedVieo,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();

            try {
                if (filePath != null) {

                    MediaPlayer mp = MediaPlayer.create(this, Uri.parse(filePath));
                    int duration = mp.getDuration();
                    mp.release();

                    if((duration/1000) > 30){
                        // Show Your Messages                        
                    }else{
                        runVideo(filePath);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

这篇关于从图库中选择视频:仅限制不超过30秒的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆