How to determine video width and height on Android

2019-03-15 11:28发布

问题:

I have a video file and I want to get width and height of video. I don't want to play it, just to get size. I've tried to use MediaPlayer:

MediaPlayer mp = new MediaPlayer();
mp.setDataSource(uriString);
mp.prepare();
int width = mp.getVideoWidth();
int height = mp.getVideoHeight();

but it returns 0 for width and height, because VideoSizeChangedEvent didn't fire yet. How can I get width and height of video?

UPD: I need API version 7

回答1:

This works in API level 10 and up:

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource("/path/to/video.mp4");
int width = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
int height = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
retriever.release();


回答2:

try with this code and let me know if any issues..

MediaMetadataRetriever retriever = new  MediaMetadataRetriever();
Bitmap bmp = null;      

try 
{
    retriever.setDataSource("...location of your video file");
    bmp = retriever.getFrameAtTime();
    videoHeight=bmp.getHeight();
    videoWidth=bmp.getWidth();
}


回答3:

This worked for me

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(final MediaPlayer mp) {
                int width = mp.getVideoWidth();
                int height = mp.getVideoHeight();
            }
        });


回答4:

API level 7 solution:

// get video dimensions
MediaPlayer mp = new MediaPlayer();
        try {
            mp.setDataSource(filename);
            mp.prepare();
            mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
                @Override
                public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

                    int orient = -1;

                    if(width < height)
                        orient = 1;
                    else
                        orient = 0;

                }
            });
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


回答5:

private static final Uri MOVIE_URI = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
private static final String[] MOVIE_PJT = new String[] {
        MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA,
        MediaStore.Video.Media.TITLE, MediaStore.Video.Media.DATE_TAKEN,
        MediaStore.Video.Media.MIME_TYPE, MediaStore.Video.Media.DURATION,
        MediaStore.Video.Media.SIZE, MediaStore.Video.Media.RESOLUTION };

try this to query from system content provider. It will return some useful info of the Videos(local files)



回答6:

This set of utilities to work with the Size abstraction in Android.

It contains an class SizeFromVideoFile.java You can use it like this:

ISize size = new SizeFromVideoFile(videoFilePath);
size.width();
size.hight();