Search code examples
androidvideostreamingvideo-streamingmedia-player

Android - Why does this video does not play?


I need to play the following video (as a stream) on Android: http://goo.gl/Dc6tzA

It meets all the requirements this page states: http://developer.android.com/guide/appendix/media-formats.html

Its codec is: H264 - MPEG4 AVC

Its container is: .mp4

I've even checked (with http://atomicparsley.sourceforge.net/) to see if the moov atom was coming before the mdat atoms and after the ftyp atom as the above page warns:

"For 3GPP and MPEG-4 containers, the moov atom must precede any mdat atoms, but must succeed the ftyp atom."

Everything is fine. But the video just does not play. It calls MediaPlayes' onPrepared callback alright, but it does not play. Moreover, mediaPlayer.getVideoWidth() and mediaPlayer.getVideoHeight() (in the code bellow) return both 0 (even though the video is 640x360).

Follow my onPrepared method:

@Override
public void onPrepared(MediaPlayer mediaplayer) {

    width = mediaPlayer.getVideoWidth();
    height = mediaPlayer.getVideoHeight();

    //Get the width of the screen
    int screenWidth = getWindowManager().getDefaultDisplay().getWidth();

    //Get the SurfaceView layout parameters
    android.view.ViewGroup.LayoutParams lp = surfaceView.getLayoutParams();

    //Set the width of the SurfaceView to the width of the screen
    lp.width = screenWidth;

    //Set the height of the SurfaceView to match the aspect ratio of the video 
    //be sure to cast these as floats otherwise the calculation will likely be 0
    lp.height = (int) (((float)height / (float)width) * (float)screenWidth);

    //Commit the layout parameters
    surfaceView.setLayoutParams(lp); 

    playPauseImageButton.setImageResource(R.drawable.ic_media_pause);

    //if (width != 0 && height != 0) {
        surfaceHolder.setFixedSize(width, height);
        seekBar.start();
        mediaPlayer.start();
    //}

    playPauseImageButton.setEnabled(true);
}

I've also set the onError callback, but it is never called. Any ideas anyone?


Solution

  • I've ended up using ExoPlayer to solve this instead of the standard MediaPlayer.