title: MediaPlayer playing sound but not video
body: <p>I'm trying to play a video from Android using a <code>SurfaceView</code>.</p>
  
  <p>I can load the video, the sound is played correctly but I can't see the video.</p>
  
  <p>Here is my activity :</p>
  
  <pre><code>public class VideoTest extends Activity implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback, OnErrorListener {
  
      private static final String TAG = "MediaPlayerDemo";
      private int mVideoWidth;
      private int mVideoHeight;
      private MediaPlayer mMediaPlayer;
      private SurfaceView mPreview;
      private SurfaceHolder holder;
      private String path;
      private Bundle extras;
      public static final String MEDIA = "media";
      private static final int LOCAL_AUDIO = 1;
      public static final int STREAM_AUDIO = 2;
      private static final int RESOURCES_AUDIO = 3;
      private static final int LOCAL_VIDEO = 4;
      public static final int STREAM_VIDEO = 5;
      private boolean mIsVideoSizeKnown = false;
      private boolean mIsVideoReadyToBePlayed = false;
      private RelativeLayout layout;
  
      /**
       *
       * Called when the activity is first created.
       */
      @Override
      public void onCreate(Bundle icicle) {
          super.onCreate(icicle);
  
          mPreview = new SurfaceView(this);
          holder = mPreview.getHolder();
          holder.addCallback(this);
          holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
          extras = getIntent().getExtras();
  
          mPreview.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
  
          layout = new RelativeLayout(this);
          layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
          layout.addView(mPreview, 0);
  
          setContentView(layout);
  
      }
  
      private void playVideo(Integer Media) {
          doCleanUp();
          try {
              mMediaPlayer = MediaPlayer.create(this, R.raw.video);
              mMediaPlayer.setDisplay(holder);
  
              mMediaPlayer.setOnBufferingUpdateListener(this);
              mMediaPlayer.setOnCompletionListener(this);
              mMediaPlayer.setOnPreparedListener(this);
              mMediaPlayer.setOnVideoSizeChangedListener(this);
              mMediaPlayer.setOnErrorListener(this);
  
              mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  
          } catch (Exception e) {
              Log.e(TAG, "error: " + e.getMessage(), e);
          }
      }
  
      public void onBufferingUpdate(MediaPlayer arg0, int percent) {
          Log.d(TAG, "onBufferingUpdate percent:" + percent + " pos : " + mMediaPlayer.getCurrentPosition() + " / " + mMediaPlayer.getDuration());
          if (mMediaPlayer.isPlaying()) {
              Log.d(TAG, "Playing");
          } else {
              mMediaPlayer.start();
          }
      }
  
      public void onCompletion(MediaPlayer arg0) {
          Log.d(TAG, "onCompletion called");
      }
  
      public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
          Log.v(TAG, "onVideoSizeChanged called");
          if (width == 0 || height == 0) {
              Log.e(TAG, "invalid video width(" + width + ") or height(" + height
                      + ")");
              return;
          }
          mIsVideoSizeKnown = true;
          mVideoWidth = width;
          mVideoHeight = height;
          if (mIsVideoReadyToBePlayed &amp;&amp; mIsVideoSizeKnown) {
              startVideoPlayback();
          }
      }
  
      public void onPrepared(MediaPlayer mediaplayer) {
          Log.d(TAG, "onPrepared called");
          mIsVideoReadyToBePlayed = true;
          if (mIsVideoReadyToBePlayed &amp;&amp; mIsVideoSizeKnown) {
              startVideoPlayback();
          }
      }
  
      public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
          Log.d(TAG, "surfaceChanged called. Width : " + j + ", height : " + k);
          holder = surfaceholder;
          mMediaPlayer.setDisplay(holder);
          mIsVideoSizeKnown = true;
          mVideoWidth = j;
          mVideoHeight = k;
          if (mIsVideoReadyToBePlayed &amp;&amp; mIsVideoSizeKnown) {
              startVideoPlayback();
          }
      }
  
      public void surfaceDestroyed(SurfaceHolder surfaceholder) {
          Log.d(TAG, "surfaceDestroyed called");
      }
  
      public void surfaceCreated(SurfaceHolder holder) {
          Log.d(TAG, "surfaceCreated called");
          playVideo(extras.getInt(MEDIA));
  
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          releaseMediaPlayer();
          doCleanUp();
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          releaseMediaPlayer();
          doCleanUp();
      }
  
      private void releaseMediaPlayer() {
          if (mMediaPlayer != null) {
              mMediaPlayer.release();
              mMediaPlayer = null;
          }
      }
  
      private void doCleanUp() {
          mVideoWidth = 0;
          mVideoHeight = 0;
          mIsVideoReadyToBePlayed = false;
          mIsVideoSizeKnown = false;
      }
  
      private void startVideoPlayback() {
          Log.v(TAG, "startVideoPlayback " + mVideoWidth + "x" + mVideoHeight);
  
          int width = mPreview.getWidth();
          int height = mPreview.getHeight();
          float boxWidth = width;
          float boxHeight = height;
  
          float videoWidth = mMediaPlayer.getVideoWidth();
          float videoHeight = mMediaPlayer.getVideoHeight();
  
          Log.i(TAG, String.format("startVideoPlayback @ video %dx%d - box %dx%d", (int) videoWidth, (int) videoHeight, width, height));
  
          float wr = boxWidth / videoWidth;
          float hr = boxHeight / videoHeight;
          float ar = videoWidth / videoHeight;
  
          if (wr &gt; hr) {
              width = (int) (boxHeight * ar);
          } else {
              height = (int) (boxWidth / ar);
          }
  
          Log.i(TAG, String.format("Scaled to %dx%d", width, height));
  
          holder.setFixedSize(width, height);
          mMediaPlayer.start();
      }
  
      public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
          Log.e(TAG, "ERROR called : " + arg1 + ", " + arg2);
          return false;
      }
  }
  </code></pre>
  
  <p>The <code>onVideoSizeChanged</code> function is never called but the <code>onPrepared</code> and <code>surfaceChanged</code> are called.
  Thus the <code>startVideoPlayback</code> function is called, but the video width and height are 0.</p>
  
  <p>My video is playing, as I can hear the sound, but nothing is displayed on the screen.
  I also tried to give raw width and height to the <code>setFixedSize</code> function of the <code>SurfaceHolder</code> object but I still don't have anything displayed.</p>
  
  <p>Can you help me?
  I'm using Android 8</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is the log I have when I'm playing a video from the resources :</p>
  
  <pre><code>WARN    info/warning (1, 35)
  WARN    info/warning (1, 44)
  DEBUG   Duration : 101248
  DEBUG   surfaceChanged called. Width : 480, height : 270
  INFO    Info (1,35)
  INFO    Info (1,44)
  DEBUG   onPrepared called
  VERBOSE startVideoPlayback 480x270
  INFO    startVideoPlayback @ video 0x0 - box 480x270
  INFO    Scaled to 480x0
  DEBUG   surfaceDestroyed called
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I tried with another video and it's working.</p>
  
  <p>Here are the specifications of the "not working" video :</p>
  
  <ul>
  <li><strong>Container</strong> : MP4 - QuickTime</li>
  <li><strong>Rate</strong> : 2 340 Kbps</li>
  <li><strong>Format</strong> : H.264/MPEG-4-AVC</li>
  <li><strong>Size</strong> : 1280*640</li>
  <li><strong>Aspect pixel</strong> : undefined</li>
  <li><strong>Image proportion</strong> : 2.000</li>
  <li><strong>Encoding profile</strong> : Baseline@L3.1</li>
  </ul>
  
  <p>Here are the specifications for the working video :</p>
  
  <ul>
  <li><strong>Container</strong> : MP4 - QuickTime</li>
  <li><strong>Rate</strong> : 537 Kbps</li>
  <li><strong>Format</strong> : H.264/MPEG-4-AVC</li>
  <li><strong>Size</strong> : 640*360</li>
  <li><strong>Aspect pixel</strong> : undefined</li>
  <li><strong>Image proportion</strong> : 16:9</li>
  <li><strong>Encoding profile</strong> : Baseline@L3.0</li>
  </ul>
  
  <p>Do you know what's wrong with the first video ?</p>