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 theJWPlayerView
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:
- 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" />
- In app/java/MainActivity.java, define
mPlayerView
to reference theJWPlayerView
in app/res/layout/activity_main.xml.
mPlayerView = findViewById(R.id.jwplayer);
- Use PlaylistItem.Builder() to create and define a
PlaylistItem
object. We strongly recommend using thehttps://
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.
- Create a
List<PlaylistItem>
object. Add thePlaylistItem
object to theList<PlaylistItem>
object.
List<PlaylistItem> playlist = new ArrayList<>();
playlist.add(playlistItem);
- Use PlayerConfig.Builder() to create a
PlayerConfig
object. Assign theList<PlaylistItem>
object to theplaylist
property of thePlayerConfig
object.
PlayerConfig config = new PlayerConfig.Builder()
.playlist(playlist)
.build();
- Set up
mPlayerView
with thePlayerConfig
object.
mPlayerView.setup(config);
- Override
onDestroy()
,onPause()
,onResume()
,onStart()
, andonStop()
. 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();
}
}
Updated about 1 year ago