Play a video from its url

If you have a video in firebase database and you want to play it in a VideoView in Sketchware, follow the steps given below.

1. Create a new Activity PlayVideoActivity.

2. In View settings remove status bar and toolbar, and set screen orientation to both portrait and landscape.

3. Create a String variable url.

4. In onCreate, set url to the video url
For example: https://firebasestorage.googleapis.com/v0/b/bikerflyer-d2ce1.appspot.com/o/bikeflyer_chat%2FVID-20200517-WA0007.mp4?alt=media&token=d5f0ab17-9660-4584-ba62-8e7c645be742

5. In onCreate event:
// Define a FrameLayout.
FrameLayout layout = new FrameLayout(this);

FrameLayout.LayoutParams layoutparams=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
layout.setLayoutParams(layoutparams);

// Define VideoView and MediaController.
final VideoView vidview = new VideoView(this);
FrameLayout.LayoutParams vvlp = 
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
vidview.setLayoutParams(vvlp);

final MediaController mediaControls = new MediaController(this); mediaControls.setAnchorView(vidview); vidview.setMediaController(mediaControls);

// Define a ProgressBar.
final ProgressBar mBufferingBar = new ProgressBar(this);

RelativeLayout.LayoutParams pblp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

pblp.addRule(RelativeLayout.CENTER_VERTICAL);

mBufferingBar.setLayoutParams(pblp);

// Define a RelativeLayout.
RelativeLayout rl = new RelativeLayout(this);

RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
rl.setLayoutParams(lparams);

// Add VideoView to FrameLayout.
layout.addView(vidview);

// Add FrameLayout and ProgressBar to RelativeLayout.
rl.addView(layout);
rl.addView(mBufferingBar);

// Set RelativeLayout as view of the Activity.
setContentView(rl);

// Set the url as the Uri of VideoView.
vidview.setVideoURI(Uri.parse(url));

// Start Video and make ProgressBar GONE when video is prepared.
vidview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mBufferingBar.setVisibility(View.GONE);
vidview.seekTo(1);
vidview.start();
}
});

6. Save and run the project.


To pause the video and to play it from saved position in beginning:
* Create a more block extra. Here declare the VideoView, an int mCurrentPosition, and define event onSaveInstanceState.
}
VideoView vidview;
private int mCurrentPosition = 0;

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save current position of VideoView in Bundle outState of onSaveInstanceState, with key "play_time".
outState.putInt("play_time", vidview.getCurrentPosition());
}
{

* In onCreate use codes as above with following changes:
//replace
final VideoView vidview = new VideoView(this);
with
vidview = new VideoView(this);
// Add following code to set current position
if (savedInstanceState != null){
mCurrentPosition = savedInstanceState.getInt("play_time");
}
// Change onPreparedListener code to
vidview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mBufferingBar.setVisibility(View.GONE);
if (mCurrentPosition > 0){
vidview.seekTo(mCurrentPosition);
} else {
vidview.seekTo(1);
}
vidview.start();
}
});

* In onPause event, pause the VideoView.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N){
vidview.pause();
}

* In onStop event, stop the VideoView
vidview.stopPlayback();


Comments

  1. Nice tutorial bro! But, can you make a tutorial about how to pick and play songs from SD Card? Cause my app crashes when I try to pick from my SD card/External Storage.

    ReplyDelete
  2. It's very useful article with inforamtive and insightful content and i had good experience with this information. We, at the CRS info solutions ,help candidates in acquiring certificates, master interview questions, and prepare brilliant resumes.Go through some helpful and rich content Salesforce Admin syllabus from learn in real time team. This Salesforce Development syllabus is 100% practical and highly worth reading. Recently i have gone through Salesforce Development syllabus and Salesforce Admin syllabus which includes Salesforce training in USA so practically designed.

    ReplyDelete

Post a Comment

Popular posts from this blog

Create Music Player app in Sketchware

A Flash Light App in Sketchware

Admob rewarded video Ads in Sketchware

Simple car racing android game in Sketchware

Firebase auth in Sketchware for Login Activity