VideoView in Sketchware
To add a VideoView to play videos picked from Sdcard follow following steps in Sketchware.
1. Create a new project in Sketchware.
2. In VIEW area add a Button button1 and a LinearLayout linear2.
3. Create a String List list, and a String variable str.
4. Add a FilePicker component fp with mime type video/*.
5. Create a more block extra.
6. In the more block extra, use an add source directly block and put codes to declare a VideoView vidview and an MediaController mediaControls.
}
VideoView vidview;
MediaController mediaControls;
{
7. In onCreate event, use add source directly blocks and put following codes:
i. Define the VideoView vidview, set it's LayoutParams, and add it to the linear 2.
vidview = new VideoView(this);
vidview.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linear2.addView(vidview);
ii. Define the MediaController, anchor it to the VideoView, and set it as the MediaController of the VideoView.
mediaControls = new MediaController(this); mediaControls.setAnchorView(vidview); vidview.setMediaController(mediaControls);
iii. Set OnCompletionListener and OnErrorListener for the VideoView.
vidview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
} });
vidview.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
showMessage("Oops An Error Occured While Playing Video!!");
return false; } });
8. In button1 onClick event use the block FilePicker pick files.
9. In the event FilePicker onFilesPicked, get file path of video picked to String str and set it as the Uri of the vidview.
Following code is used in the add source directly block.
vidview.setVideoURI(Uri.parse(str));
vidview.start();
10. Save and run the project. You can click the Button to pick any video. The picked video plays in VideoView.
* We can also set OnPreparedListener event on VideoView, and play the video when it is ready to play.
vidview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
vidview.start();
} });
In this case we need not use vidview.start(); in FilePicker onFilesPicked event.
1. Create a new project in Sketchware.
2. In VIEW area add a Button button1 and a LinearLayout linear2.
3. Create a String List list, and a String variable str.
4. Add a FilePicker component fp with mime type video/*.
5. Create a more block extra.
6. In the more block extra, use an add source directly block and put codes to declare a VideoView vidview and an MediaController mediaControls.
}
VideoView vidview;
MediaController mediaControls;
{
7. In onCreate event, use add source directly blocks and put following codes:
i. Define the VideoView vidview, set it's LayoutParams, and add it to the linear 2.
vidview = new VideoView(this);
vidview.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linear2.addView(vidview);
ii. Define the MediaController, anchor it to the VideoView, and set it as the MediaController of the VideoView.
mediaControls = new MediaController(this); mediaControls.setAnchorView(vidview); vidview.setMediaController(mediaControls);
iii. Set OnCompletionListener and OnErrorListener for the VideoView.
vidview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
} });
vidview.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
showMessage("Oops An Error Occured While Playing Video!!");
return false; } });
8. In button1 onClick event use the block FilePicker pick files.
9. In the event FilePicker onFilesPicked, get file path of video picked to String str and set it as the Uri of the vidview.
Following code is used in the add source directly block.
vidview.setVideoURI(Uri.parse(str));
vidview.start();
10. Save and run the project. You can click the Button to pick any video. The picked video plays in VideoView.
* We can also set OnPreparedListener event on VideoView, and play the video when it is ready to play.
vidview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
vidview.start();
} });
In this case we need not use vidview.start(); in FilePicker onFilesPicked event.