为什么我无法从MediaStore获取视频持续时间?

5
在我的一个设备上,以下代码无法使我的视频出现在相册中:
File file = new File(path);
Uri uri = Uri.fromFile(file);
Intent scanFileIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
activity.sendBroadcast(scanFileIntent);

所以我明确使用scanFile

android.media.MediaScannerConnection.scanFile(activity, new String[]{file.getAbsolutePath()},
                new String[]{"video/" + mMimeType}, null);

当我的视频格式为 xxx.mpeg4 时,mMimeType 的值是 mp4,结果是视频可以出现在 MediaStore 中,但我后来无法获取持续时间(返回的值始终为0)。需要对此进行帮助。

public static long[] getVideoDetail(Context context, Uri uri) {
        long[] result = new long[] {DEFAULT_VIDEO_FRAME_WIDTH, DEFAULT_VIDEO_FRAME_HEIGHT, -1};
        if(uri == null || (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()))) {
            return result;
        }
            String[] projection = new String[] {MediaStore.Video.Media.RESOLUTION, MediaStore.Video.VideoColumns.DURATION};
            Cursor cursor = null;
            boolean success = false;
            try {
                cursor = context.getContentResolver().query(uri, projection, null, null, null);
                if (cursor.moveToFirst()) {
                    String resolution = cursor.getString(0);
                    if(!StringUtils.isEmpty(resolution)) {
                        int index = resolution.indexOf('x');
                        result[0] = Integer.parseInt(resolution.substring(0, index));
                        result[1] = Integer.parseInt(resolution.substring(index + 1));
                        if(result[0] != 0 && result[1] != 0) {
                            success = true;
                        }
                        if(result[0] > result[1]) {
                            swap(result, 0, 1);
                        }
                    }
                    result[2] = cursor.getLong(1);
                    if(result[2] >= 0 && success) {
                        success = true;
                    } else {
                        success = false;
                    }
                }
                if (null != cursor) {
                    cursor.close();
                }
            }
            catch (Exception e) {
                // do nothing
            } finally {
                try {
                    if (null != cursor) {
                        cursor.close();
                    }
                } catch (Exception e2) {
                    // do nothing
                }
            }
            if (!success) {
                try {
                    ContentResolver contentResolver = context.getContentResolver();
                    String selection = MediaStore.Images.Media._ID + "= ?";
                    String id = uri.getLastPathSegment();
                    String[] selectionArgs = new String[]{ id };
                    cursor = contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, null);
                    if (cursor.moveToFirst()) {
                        String resolution = cursor.getString(0);
                        if(!StringUtils.isEmpty(resolution)) {
                            int index = resolution.indexOf('x');
                            result[0] = Integer.parseInt(resolution.substring(0, index));
                            result[1] = Integer.parseInt(resolution.substring(index + 1));
                            if(result[0] > result[1]) {
                                swap(result, 0, 1);
                            }
                        }
                        result[2] = cursor.getLong(1);
                    }
                    if (null != cursor) {
                        cursor.close();
                    }

                } catch (Exception e) {
                    // do nothing
                } finally {
                    try {
                        if (cursor != null) {
                            cursor.close();
                        }
                    } catch (Exception e) {
                        // do nothing
                    }
                }
            }
        if(result[0] <= 0) {
            result[0] = DEFAULT_VIDEO_FRAME_WIDTH;
        }
        if(result[1] <= 0) {
            result[1] = DEFAULT_VIDEO_FRAME_HEIGHT;
        }
        return result;
    }
2个回答

2

根据您的代码,我看到您正在请求投影中的持续时间。

 String[] projection = new String[] {MediaStore.Video.Media.RESOLUTION, MediaStore.Video.VideoColumns.DURATION};

现在,您只需要像下面显示的那样从cursor中检索它:

 long timeInMs = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.VideoColumns.DURATION));

0

从MediaPlayer获取帮助:

MediaPlayer mp = new MediaPlayer();
    try {
        mp.setDataSource(context, Uri.parse(uri));
    } catch (IOException e) {
        Log.d("-MS-","Cannot parse url");

        e.printStackTrace();
    }
int duration=  mp.getDuration();
String[] projection = new String[] {MediaStore.Video.Media.RESOLUTION,duration};

我总是通过MediaPlayer获取持续时间。


是的,谢谢。但我仍然想知道为什么我不能在没有“MediaPlayer”的情况下获取值? - suitianshi
尝试这个: Uri uri = Uri.parse("content://media/external/video/media/9"); Cursor cursor = MediaStore.Video.query(res, data.getData(), new String[]{MediaStore.Video.VideoColumns.DURATION}); if(cursor.moveToFirst()) { String duration = cursor.getString(0); System.out.println("Duration: " + duration); }即将持续时间获取到游标而不是字符串数组。 - Esmaeel Ibraheem
抱歉,我确认在调用getDuration之前你必须进行prepare操作。除非你修改答案,否则我无法接受它。 - suitianshi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接