Add a player to an activity

Add the JWPlayer to your Android app.


The JWPlayer is the central logic 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.



Implementation

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.jwplayer.pub.view.JWPlayerView
    android:id="@+id/jwplayerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
  1. In app/java/MainActivity.java, set your license key
LicenseUtil.setLicenseKey(this, "YOUR_LICENSE_KEY");
  1. In app/java/MainActivity.java, define mPlayerView to reference the JWPlayerView in app/res/layout/activity_main.xml.
JWPlayerView playerView = findViewById(R.id.jwplayerview);
  1. Get a reference to the JWPlayer.
JWPlayer mPlayer = mPlayerView.getPlayer();
  1. Use PlaylistItem.Builder() to create and define a PlaylistItem object.
PlaylistItem playlistItem = new PlaylistItem.Builder()
        .file("https://cdn.jwplayer.com/manifests/{MEDIA_ID}.m3u8")
        .build();

📘

TIP

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

  1. Click the name of the video in your JW Player dashboard Media Library.
  2. Under Media Summary, click the View Assets.
  3. On the Video Renditions tab in the row of a specific rendition, click to reveal the DIRECT URL.
  4. Copy the DIRECT URL.
  5. Click Close.

Hosting your videos with JW Player enables you to query the Analytics API to access SDK video metrics. Read JW Player-hosted Content to learn how to add this URL to a player.


  1. Create a List<PlaylistItem> object. Add the PlaylistItem object to the List<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 mPlayer with the PlayerConfig object.
mPlayer.setup(config);


Full Code Samples

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

    <com.jwplayer.pub.view.JWPlayerView
        android:id="@+id/jwplayer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
public class MainActivity extends AppCompatActivity {

    private JWPlayer mPlayer;

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

        LicenseUtil.setLicenseKey(this, "YOUR_LICENSE_KEY");

        JWPlayerView playerView = findViewById(R.id.jwplayer);
        mPlayer = playerView.getPlayer();

        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();

        mPlayer.setup(config);
    }

}


Did this page help you?