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.

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
  3. But sometimes the companies think that they will be on risk, if they will implement Salesforce for their business Salesforce training in Hyderabad

    ReplyDelete
  4. I want to make an Android application like YouTube Music. Can I make with this SKETCHWARE..?

    ReplyDelete
  5. With special privileges and services, UEFA BET offers opportunities for small capitalists. Together ufa with the best websites that collect the most games With a minimum deposit starting from just 100 baht, you are ready to enjoy the fun with a complete range of betting that is available within the website

    ufabet , our one another option We are a direct website, not through an agent, where customers can have great confidence without deception The best of online betting sites is that our Ufa will give you the best price

    ReplyDelete
  6. อีกทั้งเรายังให้บริการ เกมสล็อต ยิงปลา แทงบอลออนไลน์ รองรับทุกการใช้งานในอุปกรณ์ต่าง ๆ HTML5 คอมพิวเตอร์ แท็บเล็ต สมาทโฟน คาสิโนออนไลน์ และมือถือทุกรุ่น เล่นได้ตลอด 24ชม. ไม่ต้อง Downloads เกมส์ให้ยุ่งยาก ด้วยระบบที่เสถียรที่สุดในประเทศไทย

    ReplyDelete
  7. หาคุณกำลังหาเกมส์ออนไลน์ที่สามารถสร้างรายได้ให้กับคุณ เรามีเกมส์แนะนำ เกมยิงปลา รูปแบบใหม่เล่นง่ายบนมือถือ คาสิโนออนไลน์ บนคอม เล่นได้ทุกอุปกรณ์รองรับทุกเครื่องมือ มีให้เลือกเล่นหลายเกมส์ เล่นได้ทั่วโลกเพราะนี้คือเกมส์ออนไลน์แบบใหม่ เกมยิงปลา

    ReplyDelete
  8. pgslot ซึ่งเกมคาสิโนออนไลน์เกมนี้เป็นเกมที่เรียกว่าเกม สล็อตเอ็กซ์โอ คุณรู้จักเกมส์เอ็กซ์โอหรือไม่ 90% ต้องรู้จักเกมส์เอ็กซ์โออย่างแน่นอนเพราะในตอนนี้เด็กนั้นเราทุกคนมักที่จะเอาก็ได้ขึ้นมา สล็อต เล่นเกมส์เอ็กซ์โอกับเพื่อนเพื่อนแล้วคุณรู้หรือไม่ว่าในปัจจุบันนี้เกมส์เอ็กซ์โอนั้นกลายมาเป็นเกมซะลอสออนไลน์ที่ให้บริการด้วยเว็บคาสิโนออนไลน์คุณสามารถเดิมพันเกมส์เอ็กซ์โอกับเว็บคาสิโนออนไลน์ได้โดยที่จะทำให้คุณนั้นสามารถสร้างกำไรจากการเล่นเกมส์เดิมพันออนไลน์ได้เราแนะนำเกมส์ชนิดนี้ให้คุณได้รู้จักก็เพราะว่าเชื่อว่าทุก

    ReplyDelete
  9. We are the best among all security companies in London because we start our work by drawing a working plan. top security companies in London
    We have a team of certified security professionals that specialize in vulnerability assessments and can secure transportation to and from your home.

    ReplyDelete
  10. Please Sir How can I read Subtitle text into video player using Sketchware

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

Enable Fullscreen for Youtube videos in WebView

How to enable upload from webview in Sketchware?

A Flash Light App in Sketchware

List of Calendar Format symbols valid in Sketchware