Announcing our brand new Developer Guides and API documentation in beta! Click here to see the new docs.

Add a player to an activity

Last Updated: May 29, 2019

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.

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.
  2. In app/java/MainActivity.java, define mPlayerView to reference the JWPlayerView in app/res/layout/activity_main.xml.
  3. Create a PlaylistItem object named playlistItem. At the minimum, you must include the file and mediaId properties.
  4. Create a List<PlaylistItem> object called playlist.

    For example: List<PlaylistItem> playlist = new ArrayList<>();

  5. Add playlistItem to playlist.
  6. Use PlayerConfig.Builder() to create a config object that defines the properties of the player.
  7. Assign playlist to config.
  8. Set up mPlayerView with config.
  9. 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.


app/res/layout/activity_main.xml:

 <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>


app/java/MainActivity.java:

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://content.jwplatform.com/videos/123acb4e-Zy98xW76.mp4")
            .mediaId("123acb4e")
            .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();
    }
}



Use this form to provide your feedback.