What video format will play on all Android devices?
Android can play a variety of video formats, but I 开发者_如何学Pythonneed to choose one format that will work on all devices.
Do all Android 2.3 devices support exactly the same formats? i.e. if the format will play in the emulator, does that mean it will also play on all hardware? Or do different devices support different formats depending on what decoder chips they have?
If they are all the same then obviously the best format is H.264 at a high bitrate and resolution. If not, then what is the best codec/bitrate/resolution that will play on 90% of devices? Do Google provide some way of querying the device's video capabilities and choosing an appropriate format?
After testing on a lot of devices(for the video splashscreen of a very popular app). My recommendations are:
video codec : H.264
file format: .mp4
video bitrate: 256kbps
video frame/second: 24
Note:My video has no sound!!
But even with this recommendation, some videos will not work because of its resolution.
So i create a tricky code: I embed all my videos for all the density in my raw
folder, added an setOnErrorListener
to my VideoView
and i try to launch a smaller video each time an error occured.
This is my raw folder:
raw/
splashmdpi.mp4
splashhdpi.mp4
splashxhdpi.mp4
and this is my java code:
int densityoffset = 0;
VideoView video = new VideoView(this);
video.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
video.start();
}
}
video.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
densityoffset++;
String suff = getDensitySuffix(getContext(), densityoffset);
video.setVideoPath("android.resource://com.example.packagename/raw/splash"+suff);
if(offset>5)
return false;
else
return true;
}
});
String suff = getDensitySuffix(this,offset);
video.setVideoPath("android.resource://com.example.packagename/raw/splash"+suff);
private String suffix[]={"ldpi","mdpi","hdpi","xhdpi"};
/**
*Return the suffix concerning your device less offset value
**/
private String getDensitySuffix(Context ctx, int offset){
int dens = 2;
int d = getContext().getResources().getDisplayMetrics().densityDpi
if(d==DisplayMetrics.DENSITY_LOW)
dens = 0;
else
if(d==DisplayMetrics.DENSITY_MEDIUM)
dens = 1;
else
if(d==DisplayMetrics.DENSITY_HIGH))
dens = 2;
else
if(d==DisplayMetrics.DENSITY_XHIGH))
dens = 3;
return suffix[Math.max(0, dens-offset)];
}
The emulator is a poor test of codecs and isn't functional in a few areas. And yes device manufacturers may add additional codecs to their build of Android. However you may want to check out the Android Compatibility and read the Compatibility Definition Document for more details as to what is required by the manufacturer get Android Market on the device. Unfortunately a quick look through it doesn't state anything about minimum bitrate so depending on how old a version of Android you are willing to support you may have issues there.
http://developer.android.com/guide/appendix/media-formats.html
I believe you can use the MediaPlayer class to see specific capabilities.
精彩评论