Add a player to an activity (Android v3)

Add the JWPlayerView to the appropriate files of your Android app.


The JWPlayerView is the central UI component of our SDK. This class allows you to easily load new media into the player, manage video and audio playback, and register multiple event listeners that could help you with custom analytics or error handling.

πŸ“˜

Adding the JWPlayerView gives you more control over the JWPlayerView lifecycle. If you do not need this control, you can add the JWPlayerFragment or JWPlayerSupportFragment to your app instead.


Add the JWPlayerView

Use the following steps and code examples to add the JWPlayerView to the app/res/layout/activity_main.xml and app/java/MainActivity.java files of your app:

  1. In app/res/layout/activity_main.xml, add the JWPlayerView.
<com.longtailvideo.jwplayer.JWPlayerView
    xmlns:jwp="http://schemas.android.com/apk/lib/com.longtailvideo.jwplayer"
    android:id="@+id/jwplayer"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

  1. In app/java/MainActivity.java, define mPlayerView to reference the JWPlayerView in app/res/layout/activity_main.xml.
mPlayerView = findViewById(R.id.jwplayer);

  1. Use PlaylistItem.Builder() to create and define a PlaylistItem object. We strongly recommend using the https:// protocol with your video URLs.
PlaylistItem playlistItem = new PlaylistItem.Builder()
    .file("https://cdn.jwplayer.com/manifests/{media_id}.m3u8")
    .build();

πŸ“˜

For videos hosted with JWP, use the following steps to retrieve the URL of a video:

β€’ Click the name of the video on the video list page.
β€’ Click the ASSETS tab.
β€’ Under Sources, click the name of the source to reveal the Direct Link.


Hosting your videos with JWP enables you to query the Analytics API to access SDK video metrics.


  1. Create a List<PlaylistItem> object. Add the PlaylistItem object to theList<PlaylistItem> object.
List<PlaylistItem> playlist = new ArrayList<>();
playlist.add(playlistItem);

  1. Use PlayerConfig.Builder() to create a PlayerConfig object. Assign the List<PlaylistItem> object to the playlist property of the PlayerConfig object.
PlayerConfig config = new PlayerConfig.Builder()
    .playlist(playlist)
    .build();

  1. Set up mPlayerView with the PlayerConfig object.
mPlayerView.setup(config);

  1. Override onDestroy(), onPause(), onResume(), onStart(), and onStop(). This allows you to properly handle the Activity Lifecycle and to release the player from memory, when necessary.
@Override
protected void onStart() {
    super.onStart();
    mPlayerView.onStart();
}

@Override
protected void onResume() {
    super.onResume();
    mPlayerView.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    mPlayerView.onPause();
}

@Override
protected void onStop() {
    super.onStop();
    mPlayerView.onStop();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mPlayerView.onDestroy();
}


Full code samples

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <com.longtailvideo.jwplayer.JWPlayerView
        xmlns:jwp="http://schemas.android.com/apk/lib/com.longtailvideo.jwplayer"
        android:id="@+id/jwplayer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>
public class MainActivity extends AppCompatActivity {

    JWPlayerView mPlayerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPlayerView = findViewById(R.id.jwplayer);
        PlaylistItem playlistItem = new PlaylistItem.Builder()
            .file("https://cdn.jwplayer.com/manifests/{media_id}.m3u8")
            .build();

        List<PlaylistItem> playlist = new ArrayList<>();
        playlist.add(playlistItem);
        PlayerConfig config = new PlayerConfig.Builder()
            .playlist(playlist)
            .build();
        mPlayerView.setup(config);

    }
    @Override
    protected void onStart() {
        super.onStart();
        mPlayerView.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPlayerView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mPlayerView.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPlayerView.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPlayerView.onDestroy();
    }
}