Do not use MediaPlayer and MediaMetadataRetriever in Android

As my modest experience shows, the native MediaPlayer, MediaMetadataRetriever, as well as other classes that are somehow based on them (VideoView, ThumbnailUtils, for example), are extremely unreliable to use. Perhaps the problem is that they are implemented natively, and vary from version to version, but the fact remains that their behavior on different devices cannot be predicted, and this does not even depend on the power of the iron.

A good example was a small project where it was required to implement video playback. You could choose any format and codec, I conducted tests on mp4 and avi with various codecs. On all devices that came to hand, the playback went smoothly and without complaints. Older devices and OS versions <4.0 were not required to be supported, and I thought that there would be no problems with these formats. Then, on HTC desire V, it was found that several videos were not playing, the built-in player in the gallery, too. It was thought that the device is rather weak, but the surprise was that almost no test videos were played at all on the Nexus 5. Although I could not find any regularity - even with the same codecs, one file could be read, and the other could not. The files were not damaged, so only the VideoView with MediaPlayer inside could only sin.

Fortunately, good alternatives were found that work not only more reliable, but also faster:

  • FFmpeg library. You can compile it yourself using the Android NDK. Well, but the disadvantages of this approach are obvious - you need to deal with NDK, build, and you can only do this under Linux. I did not want to waste time on this.
  • FFmpegMediaPlayer. Actually, it is based on the first library, but you do not need to deal with the assembly, and an interface is already provided that is completely similar to the native MediaPlayer. Purely for verification, I tried to use it, but for some reason the video was not drawn on SurfaceView, while the exact same code, but it worked without problems with the native MediaPlayer. Perhaps this was my cant, but I could not find a quick solution.
    (UPD: As noted in the comments, this library is audio only)
  • Vitamio. Also takes FFmpeg as a basis and also provides ready-made assemblies. At the same time, it also has its own VideoView and MediaMetadataRetriever, with an interface similar to that of the native Android SDK, which greatly simplifies life when creating a player. It is possible to use your own FFmpeg builds, supports OS 2.1+ and almost all formats. I stopped at this library; no problems arose with it. Please note that in the assembly there is initially no build for x86. You can also expect that your APK will get ~ 8 megabytes fatter with this library.
  • There are other options, most of them use the same FFmpeg, but Vitamio completely suited me.

In general, I highly recommend not using standard classes even for small projects and “common” common video / audio formats. Someone may disagree and say that everything works well for him. Here you can only recommend testing on more files and devices ... using Genymotion, for example.

Also popular now: