Android Video View not playing MP4 file on some devices

21,444

The answer to this question will never be consistent across all devices or across all videos.

Whether a given video file will play in a given player depends on three things:

  • The video container format (file type).
  • The codecs the video (and potentially audio) streams are encoded with
  • Your player's support for that combination of container format and codec

The codec and player/device support for it is almost certainly the cause of the inconsistent results you've seen. (A codec, if you didn't know, is basically a repeatable mathematical formula that tells your system how to turn bits and bytes packed into a file into moving pictures(and back again, for that matter))

There are a large variety of video codecs in the video files floating around out there. Support for these codecs is wildly inconsistent just due to the history of video distribution. Many devices won't support streams encoded with certain codecs. There are a variety of reasons for this, but the most common are obscurity or licensing costs.

For example, up until a few years ago, almost everything was encoded in an .FLV container with an On2 VP6/VP7/VP8 codec. This is causing headaches today because while On2 owned these codecs, they kept a tight rein on the licenses. That didn't relax until .FLV had already begun to lose relevance, and so there is not a whole lot of (legitimate) software out there that can work with On2-encoded content.

What all of this means is that there is no silver bullet. All video will never run on all devices, at least not without the aid of video players that install and use their own codecs to decode the streams.

Needless to say, this does not include the libraries provided to you and your end users by the factory-installed Android libraries.

So, what do you do? Well, short of producing a video player that carries its own codecs, you can most effectively address the problem with a two-step approach:

  1. Target specific devices that you want your application to work on
  2. Encode your content to use use a video codec that works on all the devices you want to target. You may need to produce two copies of your video if you find that there is no codec that works across all devices you plan to support.

Today, the widest support is available with an MP4 container and a video stream encoded with the H.264 (AVC) codec. As I said, there is no silver bullet, and H.264 support is not universal by any means, but this one format will be playable more potential users than any other single choice you could make, due to its popularity and wide support in modern desktop and mobile environments.

Some tools you may find helpful:

  • MediaInfo will let you peek inside MPEG-flavored video containers to see what codecs are in use. This will be helpful in determining which devices are having trouble with which codecs.
  • FFmpeg is an encoding application that can convert your content to MP4/H.264
  • Android Supported media formats List of supported media audio/video formats.

Good luck!

Share:
21,444
sandy
Author by

sandy

Hi am a java developer

Updated on July 17, 2022

Comments

  • sandy
    sandy almost 2 years

    In my app I need to play videos from sdcard. Now it works fine on Galaxy S,Galaxy Tab2, But on some chinese tab Like "Giada" It is not working at all.

    I have 4 different activities to play videos like. First activity plays a menu video which has navigation link to other activity. Problems I am facing.

    1. First Video plays properly but looping failed and app closed.
    2. If I navigate to other activity to play another video it says "Can't Play Video" and closed Some time it plays same video but not complete and closed app in between.

    Video Extension: MP4 Resolution : 1024x600 Playing From : SDCard.

    Target Tab Specification.

    Resolution : 1024x600 Android :4.1

    Tried with Video View and SurfaceView.

    Help me out any help will be regreted.

  • Umopepisdn
    Umopepisdn over 10 years
    Awesome. So, what you want to do is find out -- through research if you can and trial-and-error if you have to -- what combination of codecs/containers will play on the device, and what quality levels (video bitrate and resolution are the big ones here) will play back smoothly.
  • Umopepisdn
    Umopepisdn over 10 years
    Once you've found the "sweet spot" -- the right combination of container, codecs, resolution, bitrate, quality level, and playback smoothness for your device, convert all of your content to that format.
  • Stav Bodik
    Stav Bodik almost 5 years
    ffmpeg command example for supported android video format for almost all devices : ffmpeg -i input.mp4 -f mp4 -r 30 -vcodec libx264 -bufsize 2k -ac 2 -ab 192k -preset veryslow -profile:v main -acodec aac output.mp4 -hide_banner