Add MediaController to VideoView

Advertisemen

Last post show a example of "VideoView example to play video from Internet" without controls. This example show adding MediaController to VideoView, to provide controls of Play/Pause, Forward and Backward.

test on Nexus 9 tablet (Emulator) running Marshmallow:

test on Nexus 7 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;

public class 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();
}

private void prepareVideo(){

myMediaController = new 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 =
new MediaPlayer.OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer arg0) {
Toast.makeText(MainActivity.this, "End of Video",
Toast.LENGTH_LONG).show();
}
};

MediaPlayer.OnPreparedListener MyVideoViewPreparedListener =
new MediaPlayer.OnPreparedListener() {

@Override
public void onPrepared(MediaPlayer mp) {

long duration = myVideoView.getDuration(); //in millisecond
Toast.makeText(MainActivity.this,
"Duration: " + duration + " (ms)",
Toast.LENGTH_LONG).show();

}
};

MediaPlayer.OnErrorListener myVideoViewErrorListener =
new MediaPlayer.OnErrorListener() {

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {

String errWhat = "";
switch (what){
case MediaPlayer.MEDIA_ERROR_UNKNOWN:
errWhat = "MEDIA_ERROR_UNKNOWN";
break;
case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
errWhat = "MEDIA_ERROR_SERVER_DIED";
break;
default: errWhat = "unknown what";
}

String errExtra = "";
switch (extra){
case MediaPlayer.MEDIA_ERROR_IO:
errExtra = "MEDIA_ERROR_IO";
break;
case MediaPlayer.MEDIA_ERROR_MALFORMED:
errExtra = "MEDIA_ERROR_MALFORMED";
break;
case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
errExtra = "MEDIA_ERROR_UNSUPPORTED";
break;
case 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();
return true;
}
};
}



For layout and uses-permission, refer last post "VideoView example to play video from Internet".

Next:
Capture frames in VideoView using MediaMetadataRetriever

Related:
Open mp4 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT and ACTION_PICK, and play in VideoView.

Advertisemen

Disclaimer: Gambar, artikel ataupun video yang ada di web ini terkadang berasal dari berbagai sumber media lain. Hak Cipta sepenuhnya dipegang oleh sumber tersebut. Jika ada masalah terkait hal ini, Anda dapat menghubungi kami disini.
Related Posts
Disqus Comments
© Copyright 2017 GENERAL INFO FOR ANDROID DEVELOPMENT - All Rights Reserved - Template Created by goomsite - Proudly powered by Blogger