问题
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:
In some cases, we are unable to read the metadata. To ensure that we get the width and height, it is best to create a Bitmap
using MediaMetadataRetriever
, then get the width and height from the created Bitmap
, as shown below:
public int getVideoWidthOrHeight(File file, String widthOrHeight) {
MediaMetadataRetriever retriever = null;
Bitmap bmp = null;
FileInputStream inputStream = null;
int mWidthHeight = 0;
try {
retriever = new MediaMetadataRetriever();
inputStream = new FileInputStream(file.getAbsolutePath());
retriever.setDataSource(inputStream.getFD());
bmp = retriever.getFrameAtTime();
if (widthOrHeight.equals("width")){
mWidthHeight = bmp.getWidth();
}else {
mWidthHeight = bmp.getHeight();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
} finally{
if (retriever != null){
retriever.release()
}if (inputStream != null){
inputStream.close()
}
}
return mWidthHeight;
}
You can call the above method like this:
// Get the video width
int mVideoWidth = getVideoWidthOrHeight(someFile, "width");
// Get the video height
int mVideoHeight = getVideoWidthOrHeight(someFile, "height");
回答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:
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();
回答6:
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)
来源:https://stackoverflow.com/questions/9077436/how-to-determine-video-width-and-height-on-android