Android VideoView can be defined as below –
"Android VideoView is used to display a video file. In android application."
Some of the popular attributes of videoView are –
S. No. | XML Attributes | Description |
1 | android:id | This is unique id of the VideoView to uniquely identify the VideoView. |
2 | android:height | Height of the VideoView. |
3 | android:width | Width of the VideoView. |
4 | android:elevation | Specifies elevation of the VideoView. |
5 | android:clickable | Specifies whether this VideoView is clickable or not. |
6 | android:focusable | Specifies whether this VideoView should take focus or not. |
7 | android:longClickable | Defines whether this VideoView should respond to long click or not. |
8 | android:onClick | Specifies what to do when this VideoView is clicked. |
9 | android:padding | Defines padding of the VideoView. |
10 | android:paddingBottom | Defines bottom padding of the VideoView. |
11 | android:paddingEnd | Defines padding to right edge of the VideoView. |
12 | android:paddingHorizontal | Defines padding to left and right edges of the VideoView. |
13 | android:paddingLeft | Defines padding to left edge of the VideoView. |
14 | android:paddingRight | Defines padding to right edge of the VideoView. |
15 | android:paddingStart | Defines padding to left edge of the VideoView. |
16 | android:paddingTop | Defines padding to top edge of the VideoView. |
17 | android:visibility | Defines visibility of the VideoView. |
At first, we will create android application. Then, we will use VideoView in this application.
Follow steps below to create new project. Please ignore the steps if you've already created a new application.
S. No. | Steps |
1 | Open Android Studio. |
2 | Click on Start a new Android Studio Project Write application name as VideoView. Then, click next button. |
3 | Select minimum SDK you need. However, we have selected 14 as minimum SDK. Then, click next button. |
4 | Then, select Empty Activity => click next => click finish. |
5 | If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit tutorial to Create a New Project to know steps in detail. |
Open res/values/strings.xml file. Then, add below code into it.
<resources>
<string name="app_name">VideoView</string>
<string name="play">Play</string>
<string name="pause">Pause</string>
<string name="paused">Paused</string>
<string name="playing">Playing</string>
</resources>
Open res/layout/activity_main.xml file. Then, add below code into it.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<VideoView
android:id="@+id/videoPlayer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/play"
android:layout_marginTop="60dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/videoPlayer" />
</android.support.constraint.ConstraintLayout>
We have, defined VideoView and Button in the activity_main.xml file. Now, we will access these VideoView and Button in java file in the application.
Open src/main/java/com.ukacademe.videoview/MainActivity.java file. Then, add below code into it.
package com.ukacademe.videoview;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView videoView = findViewById(R.id.videoPlayer);
if (videoView != null) {
String path = "android.resource://" + getPackageName() + "/" + R.raw.ukacademe;
videoView.setVideoURI(Uri.parse(path));
}
final Button button = findViewById(R.id.btnPlay);
if (button != null) {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (videoView != null) {
boolean isPlaying = videoView.isPlaying();
button.setText(isPlaying ? R.string.play : R.string.pause);
String msg = getString(isPlaying ? R.string.paused : R.string.playing);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
if (isPlaying) {
videoView.pause();
} else {
videoView.start();
}
}
}
});
}
}
}
In MainActivity.java file, we have accessed android videoView. Then, we accessed video resource that was stored in res/raw folder. After that, we have set video resource to videoView. At last, we set click listener to button that play/pause video in videoView. If video is already playing, we pause the video. otherwise, we play the video when button is clicked. Method videoView.start() is being called to play the video. Method videoView.pause() is being called to pause the video.
Since the file of AndroidManifest.xml is very important in any android application, we are also going to see the content inside this file.
Code inside src/main/AndroidManifest.xml file is as below –
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ukacademe.videoview">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
When we run the program, we will get output as shown below.