android - 来自画廊的视频选择 : Limit only videos up to 30 seconds should be selected

标签 android video duration onactivityresult

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

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

关于android - 来自画廊的视频选择 : Limit only videos up to 30 seconds should be selected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28293992/

相关文章:

android - 按返回时,AccountManagerFuture 不会抛出 OperationCanceled

ruby-on-rails - 视频不在 Rails 中播放

java - 使用 Joda 时间求和持续时间

function - 如何将 time.Duration 类型传递给 go 函数?

java - 当我使用 Genymotion 模拟器运行我的第一个 Android 应用程序时,不幸的是,它给出的错误 "app name"已经停止。请帮我

Android:ImageView 放大源图像

android - android中的视频上传问题

video - ffmpeg 在 sws_scale() 中从 YUV 到 RGB 的效果较差(为 : ffmpeg blocky chroma decoding)

python-3.x - 获取持续时间mp3 python

java - 是否可以合并同等大小的mp3格式文件,然后在Java中检索、修改每个单元文件并添加新的文件?