Android Video Player Example

Last updated Nov 01, 2019

In Android VideoView widget is used to play the videos. It can load files from various sources.

android.widget.VideoView

With Help of MediaController class Play the videos. MediaController contains media controls like play,pause,previous,next etc...

android.widget.MediaController

 

VideoView in XML file


 

 

 

VideoView class methods

setMediaController (MediaController controller)

 

Use this method to set the MediaController object to VideoView

public void setVideoURI(Uri uri)

Sets the Video file Uri to VideoView

public void start()

Use to start the VideoView to play the Video

public void stopPlayback()

Use to stops the playback

public void suspend()

suspends the playback

public void resume()

 resume the playback

public void seekTo(int millis)

seeks the current video to specified time

getDuration()

get the Video duration

getCurrentPosition()

 get the current position of the video

   

 

Methods of MediaController class

setAnchorView(View view)

setAnchorView is used to view to which controller is to anchored

show()

show the controller on the screen

show(int timeout)

set the time to show the controller

hide()

hide the controller from the screen

 

 

Code in Java

 

public class PlayetActivity extends Activity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
        VideoView videoView =(VideoView)findViewById(R.id.videoView);  
          
                //Creating MediaController  
        MediaController mc= new MediaController(this);  
            mc.setAnchorView(videoView);          
         
              //specify the location of media file  
           Uri uri=Uri.parse(path of the file);          
                
              //Setting MediaController and URI, then starting the videoView  
           videoView.setMediaController(mediaController);  
           videoView.setVideoURI(uri);          
           videoView.requestFocus();  
           videoView.start();  
                     
    }
}

 

 

Play the View from Raw Folder

Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.VideoName);

 

Play the video from external storage

Uri uri = Uri.parse(Environment.getExternalStorageDirectory()+"");

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

663 Views