Code example for MediaMetadataRetriever

Methods: MediaMetadataRetriever.setDataSource, extractMetadata

0
                default: 
                    return new MediaMetadata(durationInMilliseconds, width, height, success);
            } 
        } 
 
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try { 
            Uri uri = Uri.parse(url);
            String scheme = uri.getScheme();
            if (scheme == null || scheme.equals("file")) {
                File file = new File(uri.getPath());
                String path = file.getAbsolutePath();
                if (file.exists() && (path.startsWith("/mnt/sdcard/") ||
                        path.startsWith("/sdcard/") ||
                        path.startsWith(PathUtils.getExternalStorageDirectory()))) {
                    retriever.setDataSource(path);
                } else { 
                    Log.e(TAG, "Unable to read file: " + url);
                    return new MediaMetadata(durationInMilliseconds, width, height, success);
                } 
            } else { 
                HashMap<String, String> headersMap = new HashMap<String, String>();
                if (!TextUtils.isEmpty(cookies)) {
                    headersMap.put("Cookie", cookies);
                } 
                retriever.setDataSource(url, headersMap);
            } 
            String duration = retriever.extractMetadata(
                    MediaMetadataRetriever.METADATA_KEY_DURATION);
            String videoWidth = retriever.extractMetadata(
                    MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
            String videoHeight = retriever.extractMetadata(
                    MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
            if (duration == null || videoWidth == null || videoHeight == null) {
                return new MediaMetadata(durationInMilliseconds, width, height, success);
            } 
            durationInMilliseconds = Integer.parseInt(duration);