How to play .mp4 video in videoview in android?
Asked Answered
G

6

45

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();
Gmt answered 1/7, 2013 at 7:28 Comment(2)
It's likely that that particular mp4 encoding is not supported. I answered in a seperate answer. To test you can try another mp4, for example this one works for me: archive.org/download/Pbtestfilemp4videotestmp4/video_test.mp4Uncircumcised
i do the same but still i cant play .mp4 video 08-12 14:37:30.599: D/MediaPlayer(23633): Couldn't open file on client side, trying server side 08-12 14:37:33.095: E/MediaPlayer(23633): error (1, -2147483648) 08-12 14:37:33.096: E/MediaPlayer(23633): Error (1,-2147483648) 08-12 14:37:33.096: D/VideoView(23633): Error: 1,-2147483648 got this errorStrontium
G
41

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();
    }
});
Gmt answered 19/5, 2016 at 8:6 Comment(3)
what is mp in above snippet??Seaton
Its an object of media player.Gmt
@HirenPatel But initially It shows a black screen.Yolondayon
U
31

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.

Uncircumcised answered 1/7, 2013 at 8:14 Comment(1)
so, how to know/set video encoding techniqueMerrymerryandrew
F
23

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.

Firework answered 1/7, 2013 at 14:50 Comment(2)
i do the same but still i cant play .mp4 video 08-12 14:37:30.599: D/MediaPlayer(23633): Couldn't open file on client side, trying server side 08-12 14:37:33.095: E/MediaPlayer(23633): error (1, -2147483648) 08-12 14:37:33.096: E/MediaPlayer(23633): Error (1,-2147483648) 08-12 14:37:33.096: D/VideoView(23633): Error: 1,-2147483648 got this errorStrontium
Error msg "Can't Play this video"Figurate
S
6

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.

Shipload answered 7/11, 2015 at 19:43 Comment(3)
i encounter the same problem i have to convert my video to many format before i hit the format: use total video converter to convert the video to mp4. trust me it works like charmShipload
according to the android documentary mp4 is only supported from API Level 11 (Gingerbread)Shipload
Next time please edit you answer when you are enhancing it, rather than adding comments. I have shifted one of the comments in for you.Chrystel
Y
3

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();
     }
});
Yvonneyvonner answered 14/5, 2016 at 11:49 Comment(0)
E
1

In Kotlin you can do as

 val videoView = findViewById<VideoView>(R.id.videoView)

       // If url is from raw
   /* val url = "android.resource://" + packageName
        .toString() + "/" + R.raw.video*/

    // If url is from network
    val url = "http://www.servername.com/projects/projectname/videos/1361439400.mp4"

    val video =
        Uri.parse(url)
    videoView.setVideoURI(video)
    videoView.setOnPreparedListener{
        videoView.start()
    }
Eakin answered 23/7, 2020 at 6:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.