Video selection from gallery: Limit only videos up to 30 seconds should be selected

ぃ、小莉子 提交于 2019-12-09 20:53:12

问题


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();
            }
        }

回答1:


Try this hope it helps

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();
            }
        }


来源:https://stackoverflow.com/questions/28293992/video-selection-from-gallery-limit-only-videos-up-to-30-seconds-should-be-selec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!