Add Mediacontroller To Videoview

Posted on

Last postal service demonstrate a representative of “VideoView representative to play video from Internet” without controls. This representative demonstrate adding MediaController to VideoView, to supply controls of Play/Pause, Forward as well as Backward.

assay on Nexus nine tablet (Emulator) running Marshmallow:


assay on Nexus seven running Android 5.1.1:

MainActivity.java

package com.blogspot.android_er.androidvideoview;  import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.MediaController; import android.widget.Toast; import android.widget.VideoView;  populace shape MainActivity extends AppCompatActivity {      VideoView myVideoView;      String videoSource =             "https://sites.google.com/site/androidexample9/download/RunningClock.mp4";     Uri uriVideoSource;      MediaController myMediaController;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         myVideoView = (VideoView)findViewById(R.id.vview);          prepareVideo();     }      mortal void prepareVideo(){          myMediaController = novel MediaController(MainActivity.this);         myVideoView.setMediaController(myMediaController);          Toast.makeText(MainActivity.this, videoSource, Toast.LENGTH_LONG).show();          uriVideoSource = Uri.parse(videoSource);          myVideoView.setVideoURI(uriVideoSource);          myVideoView.setOnCompletionListener(myVideoViewCompletionListener);         myVideoView.setOnPreparedListener(MyVideoViewPreparedListener);         myVideoView.setOnErrorListener(myVideoViewErrorListener);          myVideoView.requestFocus();         myVideoView.start();      }      MediaPlayer.OnCompletionListener myVideoViewCompletionListener =             novel MediaPlayer.OnCompletionListener() {                  @Override                 populace void onCompletion(MediaPlayer arg0) {                     Toast.makeText(MainActivity.this, "End of Video",                             Toast.LENGTH_LONG).show();                 }             };      MediaPlayer.OnPreparedListener MyVideoViewPreparedListener =             novel MediaPlayer.OnPreparedListener() {                  @Override                 populace void onPrepared(MediaPlayer mp) {                      long duration = myVideoView.getDuration(); //in millisecond                     Toast.makeText(MainActivity.this,                             "Duration: " + duration + " (ms)",                             Toast.LENGTH_LONG).show();                  }             };      MediaPlayer.OnErrorListener myVideoViewErrorListener =             novel MediaPlayer.OnErrorListener() {                  @Override                 populace boolean onError(MediaPlayer mp, int what, int extra) {                      String errWhat = "";                     switch (what){                         instance MediaPlayer.MEDIA_ERROR_UNKNOWN:                             errWhat = "MEDIA_ERROR_UNKNOWN";                             break;                         instance MediaPlayer.MEDIA_ERROR_SERVER_DIED:                             errWhat = "MEDIA_ERROR_SERVER_DIED";                             break;                         default: errWhat = "unknown what";                     }                      String errExtra = "";                     switch (extra){                         instance MediaPlayer.MEDIA_ERROR_IO:                             errExtra = "MEDIA_ERROR_IO";                             break;                         instance MediaPlayer.MEDIA_ERROR_MALFORMED:                             errExtra = "MEDIA_ERROR_MALFORMED";                             break;                         instance MediaPlayer.MEDIA_ERROR_UNSUPPORTED:                             errExtra = "MEDIA_ERROR_UNSUPPORTED";                             break;                         instance MediaPlayer.MEDIA_ERROR_TIMED_OUT:                             errExtra = "MEDIA_ERROR_TIMED_OUT";                             break;                         default:                             errExtra = "...others";                      }                      Toast.makeText(MainActivity.this,                             "Error!!!\n" +                             "what: " + errWhat + "\n" +                             "extra: " + errExtra,                             Toast.LENGTH_LONG).show();                     render true;                 }             }; }  

For layout as well as uses-permission, advert terminal postal service “VideoView representative to play video from Internet“.

Next:
– Capture frames inward VideoView using MediaMetadataRetriever

Related:
– Open mp4 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT as well as ACTION_PICK, as well as play inward VideoView.