How to play .mp4 video in videoview in android?

I am working on a video player application, I want to play .mp4 video in the native video view. I am not able to play video using a URL. I am getting the error "Sorry this video cannot be played" and I am also not able to play downloaded video in the native video view either.

My code for playing video in the video view:

String mUrl = "http://www.servername.com/projects/projectname/videos/1361439400.mp4";

VideoView mVideoView  = (VideoView)findViewById(R.id.videoview)
videoMediaController = new MediaController(this);
mVideoView.setVideoPath(mUrl);
videoMediaController.setMediaPlayer(mVideoView);
mVideoView.setMediaController(videoMediaController);
mVideoView.requestFocus();
mVideoView.start();

Solution 1:

Finally it works for me.

private VideoView videoView;

videoView = (VideoView) findViewById(R.id.videoView);

Uri video = Uri.parse("http://www.servername.com/projects/projectname/videos/1361439400.mp4");
videoView.setVideoURI(video);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
  @Override       
  public void onPrepared(MediaPlayer mp) {
       mp.setLooping(true);
       videoView.start();
    }
});

Solution 2:

MP4 is just a container - the video and audio stream inside it will both be encoded in different formats.

Android natively only supports certain types of formats. This is the list here.

Make sure the video and audio encoding type is supported. Just because it says "mp4" doesn't automatically mean it should be playable.

Solution 3:

Use Like this:

Uri uri = Uri.parse(URL); //Declare your url here.

VideoView mVideoView  = (VideoView)findViewById(R.id.videoview)
mVideoView.setMediaController(new MediaController(this));       
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();

Another Method:

  String LINK = "type_here_the_link";
  VideoView mVideoView  = (VideoView) findViewById(R.id.videoview);
  MediaController mc = new MediaController(this);
  mc.setAnchorView(videoView);
  mc.setMediaPlayer(videoView);
  Uri video = Uri.parse(LINK);
  mVideoView.setMediaController(mc);
  mVideoView.setVideoURI(video);
  mVideoView.start();

If you are getting this error Couldn't open file on client side, trying server side Error in Android. and also Refer this. Hope this will give you some solution.

Solution 4:

Check the format of the video you are rendering. Rendering of mp4 format started from API level 11 and the format must be mp4(H.264)

I encountered the same problem, I had to convert my video to many formats before I hit the format: Use total video converter to convert the video to mp4. It works like a charm.

Solution 5:

I'm not sure that is the problem but what worked for me is calling mVideoView.start(); inside the mVideoView.setOnPreparedListener event callback.

For example:

Uri uriVideo = Uri.parse(<your link here>);

MediaController mediaController = new MediaController(mContext);
mediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mediaController);
mVideoView.setVideoURI(uriVideo);
mVideoView.requestFocus();

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