Creating a player instance
This article is a brief introduction to the player API to help you get quickly started.
Add a player to an Activity
There are two ways to add a player to an Activity:
-
Option 1
: Use
FlowplayerView
. -
Option 2
: Use either
FlowplayerSupportFragment
orFlowplayerFragment
, depending on whether or not your Activity inherits (directly or indirectly) from AndroidX'sFragmentActivity
.
The second method is recommended since these two Fragments wrap FlowplayerView
and handle its lifecycle automatically.
Optionally, both these Fragments can also handle orientation changes and toggle fullscreen.
Option 1: Add FlowplayerView
The FlowplayerView
is the core of the Wowza Flowplayer Android SDK. It can load a video/audio, control the playback, and subscribe to player related callbacks.
It extends FrameLayout
and can, therefore, be added either via xml or programmatically in an ordinary fashion:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.flowplayer.android.player.FlowplayerView
android:id="@+id/player_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Adding FlowplayerView
gives you more control over its lifecycle. However, if you don't need this control, we recommend using either FlowplayerFragment
or FlowplayerSupportFragment
instead.
If you decide to use FlowplayerView
instead of FlowplayerFragment
or FlowplayerSupportFragment
, then you must manage its lifecycle. There are two ways to achieve this:
-
If your Activity or Fragment inherits (directly or indirectly) from AndroidX's FragmentActivity or Fragment, respectively, then it implements
LifecycleOwner
. In this case, you can simply register your Activity's or Fragment's lifecycle by adding the following line in itsonCreate()
method:FlowplayerLifecycleObserver.registerLifecycle(lifecycle)
-
If your Activity or Fragment does not implement
LifecycleOwner
, then you must call theFlowplayerView
's lifecycle methods manually by putting the following code inside the parent Activity or Fragment:override fun onStart() { super.onStart() flowplayerView.onStart() } override fun onResume() { super.onResume() flowplayerView.onResume() } override fun onPause() { flowplayerView.onPause() super.onPause() } override fun onStop() { flowplayerView.onStop() super.onStop() } override fun onDestroy() { flowplayerView.onDestroy() super.onDestroy() }
Option 2: Add FlowplayerFragment or FlowplayerSupportFragment
Both FlowplayerFragment
and FlowplayerSupportFragment
can be added in the same way as any other Fragment, either via xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/player_fragment"
class="com.flowplayer.android.player.FlowplayerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
or programmatically:
val playerFragment = FlowplayerFragment.newInstance()
fragmentManager.beginTransaction()
.replace(R.id.player_holder, playerFragment)
.commit()
You can then get the instance of the FlowplayerView
by calling:
playerFragment.getPlayer()
Info
If you add the Fragment programmatically and you want to immediately get the player's instance,
then before playerFragment.getPlayer()
you first need to call fragmentManager.executePendingTransactions()
.
That's because the .commit()
that you called earlier, will commit the transaction asynchronously.
Prepare the player
FlowplayerView
can play media directly from a media URL that you specify.
The player can be prepared with a local or remote media URL. This is possible by preparing the player with an ExternalMedia
instance as shown below. An ExternalMedia
may optionally contain an ad schedule as well.
val externalMedia = ExternalMedia("https://link.to.a.media.file")
flowplayerView.prepare(externalMedia, true)