Getting started

This guide walks you through adding PRESTOplay for Android to your project, initializing the SDK, and playing your first stream.

Prerequisites

Add the SDK dependency

Add the Castlabs Maven repository to your project-level settings.gradle (or build.gradle) and include the SDK dependency in your module-level build.gradle.

Repository

// settings.gradle or project-level build.gradle
repositories {
    google()
    mavenCentral()
    maven {
        url 'https://mvn.players.castlabs.com/'
    }
}

Dependency

// module-level build.gradle
dependencies {
    implementation 'com.castlabs.player:castlabs-sdk:<version>'
}

Replace <version> with the SDK version you want to use.

Add the license key

Add your Castlabs license key to AndroidManifest.xml:

<application>
    <meta-data
        android:name="castlabs-license"
        android:value="<your-license-key>" />
</application>

The license is bound to your app’s package name and signing certificate SHA-1 fingerprint.

Initialize the SDK

Call PlayerSDK.init() in your Application.onCreate() method. If you use plugins, register them before calling init():

import com.castlabs.android.PlayerSDK;

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // Register plugins before init (optional)
        // PlayerSDK.register(new SubtitlesPlugin());
        PlayerSDK.init(getApplicationContext());
    }
}

Add the player view

Add a PlayerView to your activity layout:

<com.castlabs.android.player.PlayerView
    android:id="@+id/player_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Declare the activity in your manifest with configChanges so the player handles orientation changes without being recreated:

<activity
    android:name=".PlayerActivity"
    android:configChanges="orientation|keyboardHidden|screenSize" />

Start playback

Get the PlayerController from the view and open a stream with a PlayerConfig:

import com.castlabs.android.player.PlayerConfig;
import com.castlabs.android.player.PlayerView;

public class PlayerActivity extends AppCompatActivity {
    private PlayerView playerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        playerView = findViewById(R.id.player_view);

        PlayerConfig config = new PlayerConfig.Builder(
            "https://demo.cf.castlabs.com/media/prestohls/master.m3u8"
        ).get();
        playerView.getPlayerController().open(config);
    }

    @Override
    protected void onStart() {
        super.onStart();
        playerView.getLifecycleDelegate().start(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        playerView.getLifecycleDelegate().resume();
    }

    @Override
    protected void onStop() {
        super.onStop();
        playerView.getLifecycleDelegate().releasePlayer(false);
    }
}

You must delegate the activity lifecycle to the player view’s LifecycleDelegate. This ensures that resources are properly acquired and released as the activity moves through its lifecycle.

Next topic: Thumbnails
Previous topic: PRESTOplay for Android