13

Is there a way to ask an Android device what audio and video Codecs it supports for encoding?

I found devices that do not support some of the codecs listed as mandatory in http://developer.android.com/guide/appendix/media-formats.html and there seem to be devices supporting additional codec not listed there.

Marcus Wolschon
  • 2,550
  • 2
  • 22
  • 28

4 Answers4

9

That could be interesting for you:

 private static MediaCodecInfo selectCodec(String mimeType) {
     int numCodecs = MediaCodecList.getCodecCount();
     for (int i = 0; i < numCodecs; i++) {
         MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);

         if (!codecInfo.isEncoder()) {
             continue;
         }

         String[] types = codecInfo.getSupportedTypes();
         for (int j = 0; j < types.length; j++) {
             if (types[j].equalsIgnoreCase(mimeType)) {
                 return codecInfo;
             }
         }
     }
     return null;
 }

Found it here. AS you can see you get the number of installed codecs with MediaCodecList.getCodecCount();. With MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i); you get information about a specific codec out of the list. codecInfo.getName() for example tells you title/name of the codec.

Jonson
  • 171
  • 2
  • 6
4

Is there a way to ask an Android device what audio and video Codecs it supports for encoding?

I really wish there were, but there is not, at least through ICS.

Jelly Bean offers a MediaCodec class. While it does not have a "give me a list of supported codecs", it does have createEncoderByType(), where you pass in a MIME type. Presumably, that will throw a RuntimeException or return null if your desired MIME type is not supported. And I cannot promise that just because MediaCodec reports that an encoder is available that it is guaranteed to work from, say, MediaRecorder.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
3

Here is an updated code based on Jonson's answer, written in Kotlin and not using deprecated methods:

 fun getCodecForMimeType(mimeType: String): MediaCodecInfo? {
    val mediaCodecList = MediaCodecList(MediaCodecList.REGULAR_CODECS)
    val codecInfos = mediaCodecList.codecInfos
    for (i in codecInfos.indices) {
        val codecInfo = codecInfos[i]
        
        if (!codecInfo.isEncoder) {
            continue
        }
        
        val types = codecInfo.supportedTypes
        for (j in types.indices) {
            if (types[j].equals(mimeType, ignoreCase = true)) {
                return codecInfo
            }
        }
    }
    
    return null
}
anro
  • 1,300
  • 16
  • 30
3

The simplest way is using

MediaCodecList(MediaCodecList.ALL_CODECS).codecInfos

It returns a array of all encoders and decoders available on your devices like this image:

image

And then, you can use filter to query the specific encoders and decoders you are looking for. For example:

MediaCodecList(MediaCodecList.ALL_CODECS).codecInfos.filter {
    it.isEncoder && it.supportedTypes[0].startsWith("video")
}

This returns all available video encoders.

tasy5kg
  • 31
  • 4