-> We will make an app which has a VideoView and Buttons to play and stop the video.
What we will need ?
- VideoView and Buttons in activity_main.xml
- Code Additions in MainActivity.java
Step 2 : Give a name (VideoDemo) to your Application and a suitable package name (mia). Then click on Next continuously, then click Finish.
Step 3 : Make a new folder named raw in res. Right-click on res->New->Folder. Name it raw. Now copy the video file (video.mp4) you want to use for VideoView in raw folder.
Step 4 : Drag VideoView and 2 Buttons in activity_main.xml OR else copy the code below in it.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <VideoView android:id="@+id/videoview" android:layout_width="fill_parent" android:layout_height="318dp" /> <LinearLayout android:layout_width="match_parent" android:gravity="center" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/playvideoplayer" android:layout_width="100dp" android:layout_height="100dp" android:text="PLAY" /> <Button android:id="@+id/stopvideoplayer" android:layout_width="100dp" android:layout_height="100dp" android:text="STOP" /> </LinearLayout> </LinearLayout>
Step 5 : Open MainActivity.java from src->com.mia.videodemo. Copy the code in it.
MainActivity.java
package com.mia.videodemo; import android.app.Activity; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.*; public class MainActivity extends Activity{ VideoView mVideoView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button buttonPlayVideo = (Button)findViewById(R.id.playvideoplayer); Button buttonStopVideo = (Button)findViewById(R.id.stopvideoplayer); mVideoView = (VideoView)findViewById(R.id.videoview); String uriPath = "android.resource://com.mia.videodemo/"+R.raw.video; Uri uri = Uri.parse(uriPath); mVideoView.setVideoURI(uri); mVideoView.requestFocus(); buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { mVideoView.start(); }}); buttonStopVideo.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { mVideoView.stopPlayback(); }}); } }
Step 6 : Run the Project.
Explanation of the Code :
Explanation from activity_main.xml :
Explanation from MainActivity.java :
- We connect the variables of Play Button (buttonPlayVideo), Stop Button (buttonStopVideo) and VideoView (mVideoView) with elements in layout.
Button buttonPlayVideo = (Button)findViewById(R.id.playvideoplayer); Button buttonStopVideo = (Button)findViewById(R.id.stopvideoplayer); mVideoView = (VideoView)findViewById(R.id.videoview);
- Store the path of the video in uriPath variable. Convert it into Uri type (It is converted to url)
String uriPath = "android.resource://com.mia.videodemo/"+R.raw.video; Uri uri = Uri.parse(uriPath); mVideoView.setVideoURI(uri); mVideoView.requestFocus();
buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { mVideoView.start(); }});
- buttonStopVideo uses stopPlayback() command for stopping video on clicking the button in onClick().
buttonStopVideo.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { mVideoView.stopPlayback(); }});
Stay Tuned with Made In Android
I was searching for this tutorial..I got it thnk u
ReplyDeleteThis code is very simple.....This button functions are very easy..But confused in Plat Video....The media controller post is better than this........
ReplyDeleteYes this tutorial is just to explain functions on VideoView
Delete