Search code examples
androidandroid-mediaplayer

Android MediaPlayer not playing mp4 video pre Lollipop


I'm trying to play an mp4 video rendered on a TextureView, it's working on API LEVEL >= 21 (after Lollipop), but on previous versions it's not playing. I'm getting the following error in the logcat: error (1, -2147483648). I think this is an unknown error (as far I know).

The file is loaded from the assets folder.

private MediaPlayer mMediaPlayer;

@Override
protected void onCreate(Bundle savedState) {
   super.onCreate(savedState);

   TextureView textureView = (TextureView) findViewById(R.id.textureView);
   textureView.setSurfaceTextureListener(this);       
}

@Override
public void onSurfaceTextureAvailable(SurfaceTexture txtSurface, final int width, final int height) {
   Surface surface = new Surface(txtSurface);

   mMediaPlayer = new MediaPlayer();
   AssetFileDescriptor afd = getAssets().openFd("video/test.mp4");
   mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
   mMediaPlayer.setSurface(surface);
   mMediaPlayer.setLooping(false);
   mMediaPlayer.setVolume(0f, 0f);

   mMediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);

   mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
               mMediaPlayer.start();
            }
   });
   mMediaPlayer.prepareAsync();
}

The surface's size is: 768x1280 and the video size is: 350x500 px. I have also tried to resize the video (creating new videos) in different sizes: 768x1280, 1080x1920 and 4k. What's causing the error? The aspect ratio or the different device/video resolution?

I'm getting the following messages in logcat while the file is getting prepared:

04-30 17:56:01.752 432-432/? V/FFmpegExtractor: SniffFFMPEG
04-30 17:56:01.752 432-432/? I/FFmpegExtractor: android-source:0xb83b8e00
04-30 17:56:01.752 432-432/? D/FFMPEG: android source begin open
04-30 17:56:01.752 432-432/? D/FFMPEG: android open, url: android-source:0xb83b8e00
04-30 17:56:01.752 432-432/? D/FFMPEG: ffmpeg open android data source success, source ptr: 0xb83b8e00
04-30 17:56:01.752 432-432/? D/FFMPEG: android source open success
04-30 17:56:01.800 432-432/? I/FFMPEG: Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'android-source:0xb83b8e00':
04-30 17:56:01.800 432-432/? I/FFMPEG:   Metadata:
04-30 17:56:01.800 432-432/? I/FFMPEG:     major_brand     : mp42
04-30 17:56:01.800 432-432/? I/FFMPEG:     minor_version   : 0
04-30 17:56:01.800 432-432/? I/FFMPEG:     compatible_brands: mp42mp41
04-30 17:56:01.800 432-432/? I/FFMPEG:     creation_time   : 2017-04-30 21:22:53
04-30 17:56:01.800 432-432/? I/FFMPEG:   Duration: 00:00:29.50, start: 0.000000, bitrate: 2522 kb/s
04-30 17:56:01.800 432-432/? I/FFMPEG:     Stream #0:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, smpte170m), 350x500, 2517 kb/s, 30 fps, 30 tbr, 30k tbn, 60 tbc (default)
04-30 17:56:01.800 432-432/? I/FFMPEG:     Metadata:
04-30 17:56:01.800 432-432/? I/FFMPEG:       creation_time   : 2017-04-30 21:22:53
04-30 17:56:01.800 432-432/? I/FFMPEG:       handler_name    : Alias Data Handler
04-30 17:56:01.800 432-432/? I/FFmpegExtractor: FFmpegExtrator, url: android-source:0xb83b8e00, format_name: mov,mp4,m4a,3gp,3g2,mj2, format_long_name: QuickTime / MOV
04-30 17:56:01.800 432-432/? I/FFmpegExtractor: list the formats suppoted by ffmpeg: 
04-30 17:56:01.800 432-432/? I/FFmpegExtractor: ========================================
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: format_names[00]: mpeg
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: format_names[01]: mpegts
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: format_names[02]: mov,mp4,m4a,3gp,3g2,mj2
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: format_names[03]: matroska,webm
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: format_names[04]: asf
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: format_names[05]: rm
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: format_names[06]: flv
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: format_names[07]: swf
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: format_names[08]: avi
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: format_names[09]: ape
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: format_names[10]: dts
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: format_names[11]: flac
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: format_names[12]: ac3
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: format_names[13]: wav
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: format_names[14]: ogg
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: format_names[15]: vc1
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: format_names[16]: hevc
04-30 17:56:01.800 432-432/? I/FFmpegExtractor: ========================================
04-30 17:56:01.800 432-432/? V/FFmpegExtractor: major_brand tag is:mp42
04-30 17:56:01.800 432-432/? D/FFmpegExtractor: suppoted codec(h264) by official Stagefright
04-30 17:56:01.800 432-432/? D/FFMPEG: android source close
04-30 17:56:01.800 432-432/? I/FFmpegExtractor: sniff through BetterSniffFFMPEG success
04-30 17:56:01.800 432-432/? D/FFmpegExtractor: ffmpeg detected media content as 'video/mp4' with confidence 0.08
04-30 17:56:01.804 432-432/? I/SampleTable: There are reordered frames present.
04-30 17:56:01.816 432-15591/? I/OMXCodec: [OMX.google.h264.decoder] AVC profile = 77 (Main), level = 31
04-30 17:56:01.816 432-15591/? I/OMXCodec: [OMX.google.h264.decoder] video dimensions are 320 x 240
04-30 17:56:01.816 432-15591/? I/OMXCodec: [OMX.google.h264.decoder] Crop rect is 320 x 240 @ (0, 0)

The video was created using Adobe After Effects and saved as .AVI file, after that imported to Adobe Premier Pro and exported to .mp4(H.264)


Solution

  • It looks like that the exported file doesn't have a valid profile. The listed media formats shows as that the only supported profile is the Baseline Profile for H.264.

    To add a baseline profile, you need to add it by downloading ffmpeg and creating a new mp4 file with the following command line:

    ffmpeg.exe -i your_file.mp4 -c:v libx264 -profile:v baseline -level 1 new_file_name.mp4