How to play .mp4 video in videoview in android?

前端 未结 6 1870
名媛妹妹
名媛妹妹 2020-12-01 02:47

I am working on video player application, I want to play .mp4 video in native videoview. I am not able to play

相关标签:
6条回答
  • 2020-12-01 03:13

    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)
         {
              mVideoViewPeekItem.start();
         }
    });
    
    0 讨论(0)
  • 2020-12-01 03:15

    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.

    0 讨论(0)
  • 2020-12-01 03:16

    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.

    0 讨论(0)
  • 2020-12-01 03:19

    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()
        }
    
    0 讨论(0)
  • 2020-12-01 03:23

    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.

    0 讨论(0)
  • 2020-12-01 03:37

    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.

    0 讨论(0)
提交回复
热议问题