【问题标题】:Why I cant't get the video duration from MediaStore?为什么我无法从 MediaStore 获取视频时长?
【发布时间】:2014-12-07 05:38:04
【问题描述】:

在我的一台设备上,以下代码无法让我的视频出现在图库中:

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

【问题讨论】:

    标签: android mediastore android-mediascanner


    【解决方案1】:

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

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

    现在您只需从cursor 中检索它,如下所示:

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

    【讨论】:

      【解决方案2】:

      从 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 就无法获得价值?
      • 试试这个:Uri uri = Uri.parse("content://media/external/video/media/9");光标 cursor = MediaStore.Video.query(res, data.getData(), new String[]{MediaStore.Video.VideoColumns.DURATION}); if(cursor.moveToFirst()) { 字符串持续时间 = cursor.getString(0); System.out.println("持续时间:" + 持续时间);即获取光标而不是字符串数组的持续时间。
      • 抱歉,我确认您必须在拨打getDuration 之前prepare。在您编辑答案之前,我不会接受它。
      猜你喜欢
      • 1970-01-01
      • 2021-02-21
      • 2015-08-17
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多