Android TV
The latest THEOplayer Android SDK (5.0.0+) is compatible both with Android mobile and Android TV. We do not have a separate SDK for each platform.
You can follow the guide: Getting started on Android.
This guide will help you to integrate the THEOplayer Android SDK into your project, and cover the following topics:
The THEOplayer Android SDK can be used for Android TV devices using Android 5.0+ (minSdkVersion 21).
Maven / Gradle / Jitpack
THEOplayer v2.83.0 and above can be managed through Jitpack. Refer to https://github.com/THEOplayer/theoplayer-sdk-android for more information.
Obtain a THEOplayer Android SDK through the THEOplayer Developer Portal at https://portal.theoplayer.com.
LICENSE
string handy, as depicted in the screenshot below, because you'll need it when configuring your video player.
Manually importing the THEOplayer Android SDK Library can be done following these steps:
Additionally, you can import the sources jar to be able to immediately see the API java source files with their documentation in Android Studio. Follow the following after successfully importing the library steps for that:
In order to use THEOplayer in an Android TV app, you will need the THEOplayerView as the central component. This class can be loaded in a Layout and this will create a new player object for you to interact with.
Important:
public class MainActivity extends Activity
// ...
@Override
protected void onPause() {
super.onPause();
tpv.onPause();
}
@Override
protected void onResume() {
super.onResume();
tpv.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
tpv.onDestroy();
}
// ...
}
When using XML to set your layout, you can add the following code to your XML file:
<com.theoplayer.android.api.THEOplayerView
android:id="@+id/theoplayer_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Once you have the THEOplayerView in your layout, you can get a reference to it by using the findViewById-method.
In addition to setting the INTERNET
permission in AndroidManifest.xml
,
you also need to configure your license string in this file, as demonstrated in the snippet below
through the <meta-data ...
tag.
<application>
<meta-data
android:name="THEOPLAYER_LICENSE"
android:value="your_license_here" />
</application>
Don't forget to swap your_license_here
with your license string, as mentioned in the "Prerequisites".
Next to using XML to configure your view, you can also instantiate the view programmatically in Java. This can be done in the following way:
THEOplayerConfig playerConfig = new THEOplayerConfig.Builder()
.chromeless(false)
.license("your_license_here")
.build();
THEOplayerView tpv = new THEOplayerView(activity, playerConfig);
Don't forget to swap your_license_here
with your license string, as mentioned in the "Prerequisites".
This view can then be placed inside another view and positioned in your layout.
Once a player is created and set in your view, you can start interacting with the player instance using the THEOplayer API.
Create a SourceDescription object and set the player's source:
SourceDescription sourceDescription = SourceDescription.Builder
.sourceDescription("https://cdn.theoplayer.com/video/elephants-dream/playlist.m3u8")
.ads(
THEOplayerAdDescription.Builder.adDescription("https://cdn.theoplayer.com/demos/preroll.xml")
.timeOffset("10")
.skipOffset("3")
.build())
.poster("http://cdn.theoplayer.com/video/big_buck_bunny/poster.jpg")
.build();
tpv.getPlayer().setSource(sourceDescription);
Notice how the getPlayer()
method returns a Player
.
You use the Player
interface to set a video stream, attach event listeners, configure autoplay, etc.
This example shows you how to listen to the player play event.
EventListener<PlayEvent> eventListener = new EventListener<PlayEvent>() {
@Override
public void handleEvent(PlayEvent event) {
System.out.println(event.getCurrentTime());
}
};
tpv.getPlayer().addEventListener(PlayerEventTypes.PLAY, eventListener);
This example shows you how to remove an event listener of the play event.
tpv.getPlayer().removeEventListener(PlayerEventTypes.PLAY, eventListener);