If you've developed an existing Android application with a video player, you may be looking for ways to enhance the video playback experience for your users. One solution to consider is using api.video player: customizable design, advanced playback features, and easy integration.
This tutorial will demonstrate how to easily use your player to play video from api.video. Let’s go! ▶️
Before getting started
To help you migrate your video content to api.video, we’ve built a tool that allows you to quickly and easily import your videos from another provider (e.g. Dropbox or Vimeo) to your api.video dashboard. All you need is an API key and the provider’s access token. You can get started with a few clicks.
Extensions
In this tutorial, we will often talk about extensions. If you are not familiar with the concept of extensions, let me explain: it is a way to extend a class or an interface with new functionality. And… it is a kind of magic 🪄
Here is a quick example:
val String.isInteger: Boolean
get() = toIntOrNull() != null
Then, you can use it as a regular property:
"My_String".isInteger // -> False
"100".isInteger // -> True
Installation
In your module build.gradle
, add
dependencies {
implementation 'video.api:android-player:X.X.X'
}
where X.X.X
is the latest version of api.video Android player.
Video Options
On api.video, each video is identified by a unique identifier called video Id. You can find the video Id in the dashboard or in the video list. In this tutorial, replace YOUR_VIDEO_ID
with the video Id of your video.
To set an api.video video Id, you have to pass it through a VideoOptions
instance:
val videoOptions = VideoOptions("YOUR_VIDEO_ID", VideoType.VOD /* For VOD, or VideoType.LIVE for Live */)
If you are using private videos you will also have to pass the private token.
A private token is a unique token that can be used only once to access a specific private video.
val videoOptions = VideoOptions("YOUR_VIDEO_ID", VideoType.VOD /* For VOD, or VideoType.LIVE for Live */, "YOUR_PRIVATE_TOKEN")
Then, to simplify migration, we have made some specific extensions for 3 main Android players:
- ExoPlayer
- VideoView
- MediaPlayer
Extensions for ExoPlayer
ExoPlayer
provides 2 main ways to play a video on your ExoPlayer
instance:
addMediaSource
oraddMediaItem
: adds the video to the playlistsetMediaSource
orsetMediaItem
: clears the playlist and adds the video
With the api.video Android player Exoplayer extensions, you can directly use these methods with a VideoOptions
method.
val exoplayer = ExoPlayer.Builder(context).build() // You already have that in your code
exoplayer.addMediaSource(videoOptions)
// Or
exoplayer.setMediaSource(videoOptions)
Extensions for VideoView
The api.video Android player provides extensions to play a new video with a VideoView:
val videoView = binding.videoView // You already have that in your code
videoView.setVideo(videoOptions)
Extensions for MediaPlayer
The api.video Android player provides extensions to play a new video with a MediaPlayer:
val mediaPlayer = MediaPlayer() // You already have that in your code
mediaPlayer.setDataSource(context, videoOptions)
Private video
The extension will send a request for the private videos to get the token session. If this request fails, the error will be returned in the onError
callback of the previous methods.
If you want to check that no error has happened, write:
val exoplayer = ExoPlayer.Builder(context).build() // You already have that in your code
exoplayer.addMediaSource(videoOptions) { error -> Log.e(TAG, "An error occurred", error)}
Conclusion
Let’s go. Let’s play video from api.video from your own Android video player now 🎉🎉🎉🎉
Thibault Beyou
Senior Mobile Developer
Follow our latest news by subscribing to our newsletter
Create your free account
Start building with video now