'How to play video from URL in videoview in Android?

I am trying to implement video player in Android using VideoView. I have video uploaded on server I am fetching video from the server and trying to play in VideoView. I have fetched video successfully but unable to play in player.

Below is my code:

    MediaController mediacontroller = new MediaController(getActivity());
    mediacontroller.setAnchorView(videoView);

    videoView.setMediaController(mediacontroller);
   
    videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
        
            videoView.setVideoURI(Uri.parse(video_url));
         
            videoView.start();
        }
    });

    videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {

            Toast.makeText(getActivity(),"Error",Toast.LENGTH_SHORT).show();
            return false;
        }
    });

What am I doing wrong?



Solution 1:[1]

You need to set video path url also before setting media controller to videoview like this

Uri uri = Uri.parse(path);
           videoView.setVideoURI(uri);
           videoView.start();

Example

     videoView.setVideoURI(Uri.parse(video_url));       
      videoView.start();
     MediaController mediacontroller = new MediaController(getActivity());
        mediacontroller.setAnchorView(videoView);
    
        videoView.setMediaController(mediacontroller);
       ProgressBar progressBar =findViewById(R.id.progressbar);
        progressBar.setVisibility(View.VISIBLE);
        videoView.setOnCompletionListener(new     MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
            
               
            }
        });
    
        videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
    
                Toast.makeText(getActivity(),"Error",Toast.LENGTH_SHORT).show();
                return false;
            }
        });
   videoView.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                // TODO Auto-generated method stub
                mp.start();
                mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
                    @Override
                    public void onVideoSizeChanged(MediaPlayer mp, int arg1,
                            int arg2) {
                        // TODO Auto-generated method stub
                        progressBar.setVisibility(View.GONE);
                        mp.start();
                    }
                });
            }
        });

Add this progressbar widget in xml like this

<ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone" />

Update:- Added progress bar while playing video

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1