Search code examples
androidvideoandroid-mediaplayervideo-processing

Android two MediaPlayers at the same time skipping


I have an app which plays two videos at the same time. On a MotoG (Android 4.4.4) if the app is playing two videos which were generated on an Android device, it works fine. If it is playing a single video generated on iOS, it works fine.

But if it is playing 1 video generated on iOS and a second video from either iOS or Android, the player goes crazy and skips and stutters and just doesn't play back properly.

There is a minor difference between the two file formats, but since both files play properly by themselves, I think this is something that should be working on Android. Below is the ffmpeg info for the different video files.

In case it makes a difference, the MediaPlayers are hooked up to display on a SurfaceHolder which comes from a SurfaceView in the layout xml.

Any help would be appreciated!

File created on iOS:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'createdOnIOS.mp4': Metadata: major_brand : mp42 minor_version : 1 compatible_brands: mp41mp42isom creation_time : 2015-02-20 08:01:30 Duration: 00:00:06.34, start: 0.000000, bitrate: 1577 kb/s Stream #0:0(und): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p(tv, smpte170m), 360x480, 1509 kb/s, 29.98 fps, 29.97 tbr, 600 tbn, 1200 tbc (default) Metadata: creation_time : 2015-02-20 08:01:30 handler_name : Core Media Video Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 63 kb/s (default) Metadata: creation_time : 2015-02-20 08:01:30 handler_name : Core Media Audio

File created on Android:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'createdOnAndroid.mp4': Metadata: major_brand : isom minor_version : 0 compatible_brands: isom3gp4 creation_time : 2015-02-20 08:10:17 Duration: 00:00:06.08, start: 0.000000, bitrate: 6096 kb/s Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 720x480, 6002 kb/s, SAR 65536:65536 DAR 3:2, 24.68 fps, 90k tbr, 90k tbn, 180k tbc (default) Metadata: rotate : 90 creation_time : 2015-02-20 08:10:17 handler_name : VideoHandle encoder : MOTO Side data: displaymatrix: rotation of -90.00 degrees Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default) Metadata: creation_time : 2015-02-20 08:10:17 handler_name : SoundHandle


Solution

  • Figured it out! (Well sort of.)

    On the android MediaPlayer there is a callback for OnVideoSizeChanged. I use that callback to adjust the UI to make sure the video aspect ratio displays correctly. For some reason, on the iOS videos, adjusting the size of video view was causing OnVideoSizeChanged to get called again. Which was causing the views to get updated, which was calling OnVideoSizeChanged... and thus causing the view to not have good performance.

    In short, the fix was:

    public void onVideoSizeChanged(MediaPlayer mp, int width, int height)
    {
      if(mViewSize == null || mViewSize.x != width ||
                              mViewSize.y != height)
      {
        mViewSize = new Point(width, height);
        updateViews();
      }
    }
    

    I'm still not sure why the android videos weren't causing this behavior, but at least I figured out how to fix my immediate issue.