Skip to content

Instantly share code, notes, and snippets.

@wangchauyan
Created February 19, 2017 13:33
Show Gist options
  • Save wangchauyan/8c8fe2697d9131fd683e3fe9aa59dd4b to your computer and use it in GitHub Desktop.
Save wangchauyan/8c8fe2697d9131fd683e3fe9aa59dd4b to your computer and use it in GitHub Desktop.
Check How Many MediaCodec Instance Your Device Support
public static int getMaxCodecInstanceByName(String name) {
final Vector<MediaCodec> codecs = new Vector<>();
final MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, 1920, 1080);
MediaCodec codec = null;
for (int i = 0; i < max; i++) {
try {
codec = MediaCodec.createDecoderByType(MediaFormat.MIMETYPE_VIDEO_AVC);
codec.configure(format, null, null, 0);
codec.start();
codecs.add(codec);
codec = null;
}
catch (IllegalArgumentException e) {
e.printStackTrace();
break;
}
catch (IOException e) {
e.printStackTrace();
break;
}
catch (Exception e) {
e.printStackTrace();
break;
}
finally {
if (codec != null) {
codec.release();
codec = null;
}
}
}
final int actualMax = codecs.size();
for (int i = 0; i < actualMax; i++) {
codecs.get(i).release();
}
codecs.clear();
return actualMax;
}
@wangchauyan
Copy link
Author

Lol, I have to admit you are right.
IMO, I just don't want to use magic numbers in the project.
Cause that might cause some accidents if other developers don't know what the number is for.
I am not saying your code is not ok or bad (I basically love that cause it's more clear), just try to figure out if there is a better way we can do :)

But anyway, you are the first one (and probably the only one) who is interested in this example and are willing to discuss it.
Really appreciate that. Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment