How to determine video width and height on Android

2019-03-15 11:51发布

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

6条回答
我命由我不由天
2楼-- · 2019-03-15 12:03

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楼-- · 2019-03-15 12:06

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();
查看更多
乱世女痞
4楼-- · 2019-03-15 12:08

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();
查看更多
Summer. ? 凉城
5楼-- · 2019-03-15 12:09
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楼-- · 2019-03-15 12:19

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();
        }
查看更多
放我归山
7楼-- · 2019-03-15 12:20

This worked for me

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(final MediaPlayer mp) {
                int width = mp.getVideoWidth();
                int height = mp.getVideoHeight();
            }
        });
查看更多
登录 后发表回答