MediaController in VideoView
A MediaController class is used to offer controls for video playback. If a video is merely played using the VideoView class, the user has no control over the video's playback, which will continue until the film's conclusion is reached. This problem can be solved by connecting a MediaController instance to the VideoView instance.
The Media Controller will then present the user with a range of controls for managing playback (seeking backward/forward and pausing in the video timeline).
Let's head over to some of the most effective methods of the MediaController class that are used to control playing.
- setAnchorView(View view): setAnchorView specifies the view to which the controller will be anchored. This determines where the controls appear on the screen.
- show(): This function shows the controller on the screen.
- show(int timeout): This function sets the time to show the controller on the screen.
- hide(): This technique is used to hide the controls from view on the screen.
- isShowing(): This function returns a Boolean value indicating whether or not the controls are visible to the user at the time.
For more information, head out to official documentation to read about more methods and use cases in detail.
Example of VideoView
The following is an example of a VideoView in Android. We play a video in a video view using a Media Controller and execute set on error and completion listener events and show Toast when the video is done, or an issue occurs while playing the video.
The changed main activity file MainActivity.kt is shown below.
Code:
package com.codingninjas.ui
import android.media.MediaPlayer
import android.media.MediaPlayer.OnCompletionListener
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
var button: Button? = null
var editText: EditText? = null
var videoView: VideoView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button = findViewById<View>(R.id.btn) as Button
editText = findViewById<View>(R.id.eT) as EditText
videoView = findViewById<View>(R.id.vV) as VideoView
button!!.setOnClickListener {
val url = editText!!.text.toString()
val mediaController = MediaController(this)
mediaController.setAnchorView(videoView)
val uri: Uri = Uri.parse(url)
videoView!!.setMediaController(mediaController)
videoView!!.setVideoURI(uri)
videoView!!.requestFocus()
videoView!!.start()
videoView!!.setOnCompletionListener(OnCompletionListener {
Toast.makeText(applicationContext, "Thanks for Watching!", Toast.LENGTH_SHORT)
.show()
})
videoView!!.setOnErrorListener(MediaPlayer.OnErrorListener { mp, what, extra ->
Toast.makeText(
applicationContext,
"Can't Play Video",
Toast.LENGTH_SHORT
).show()
false
})
}
}
}
In this step, we open MainActivity and add the code to start the video view and a MediaController object to control the video playback.
In this class, we also establish the URI for the video, execute set on error and completion listener events, and show a Toast message when the movie is finished, or an error occurs while playing it.
Our main_activity.xml looks like the code snippet given below.
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:paddingRight="8dp"
android:paddingBottom="8dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="VideoView"
android:textSize="35sp" />
<EditText
android:id="@+id/eT"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_below="@+id/tv"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp"
android:focusable="true"
android:hint="Enter URL"
android:importantForAutofill="auto"
android:inputType="text"
android:text="https://download.samplelib.com/mp4/sample-5s.mp4"
android:textColorHighlight="#FD8EB4"
android:textColorHint="#FF5252" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/eT"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/eT"
android:text="Enter" />
<VideoView
android:id="@+id/vV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
And finally, we will use internet permission in our AndroidManifest.xml file to load URLs through the internet.
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.codingninjas.ui">
<uses-permission android:name="android.permission.INTERNET" />
<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/Theme.UI">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
When you launch the app, you will see a video playing while the app opens. When you click on the Video button, the Media Controller will display on the screen.
We will pass the URL of the mp4 video, and it will load and start playing it as soon as you press enter.
It will also render the Media Controller class for changing seek a position within the video or changing its speed. For that, you have to create your own Custom Class. We have set a video completion listener which will print Thanks for Watching! toast when the video is completed. Also, we have employed a video error listener which will be called when the URL is incorrect or the video can’t be played due to mismatch extension support or defective video. It will print Can't Play Video toast when this listener is called.
FAQs
-
How do I mute video on Android?
Ans: If you want to access the MediaPlayer of a VideoView, you must use MediaPlayer. MediaPlayer and OnPreparedListener. Then, in OnCompletionListener, you can invoke MediaPlayer. To set the volume to 0, use the setVolume(0f, 0f) method.
-
How do I get raw videos on Android?
Ans: Place the video in your project's res/raw folder. Make a raw folder in the res folder. It must be in a supported format (MP4, 3GP, WMV) and labeled with lower case numbers, underscores, and dots in the filename, for example, video_file_360p.mp4.
-
What is an ExoPlayer?
Ans: ExoPlayer is an app-level media player built on Android's low-level media APIs. ExoPlayer has several advantages over Android's built-in MediaPlayer. Many of the similar media formats as MediaPlayer are supported and adaptive formats such as DASH and SmoothStreaming.
Key Takeaways
In this article, we have extensively discussed VideoView in Android and its essential methods.
You can head over to our Android Development Course on the Coding Ninjas Website to dive deep into Android Development and build future applications.
We hope that this blog has helped you enhance your knowledge regarding VideoView and if you would like to learn more, check out our articles on SearchView. Do upvote our blog to help other ninjas grow. Happy Coding!