How to play .mp4 video in videoview in android?

Hiren Patel picture Hiren Patel · Jul 1, 2013 · Viewed 121.1k times · Source

I am working on video player application, I want to play .mp4 video in native videoview. I am not able to play video using url. I am getting error Sorry this video cannot be played and also I am not able to play downloaded video in native videoview.

My code for playing video in videoview:

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();

Kindly share your ideas for the same.

Thanks.

Answer

Hiren Patel picture Hiren Patel · May 19, 2016

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();
    }
});

Hope this would help others.