Creating a player instance

This section gives a brief introduction to the player API in order to help you get quickly started.

Add a player to an Activity

There are two ways to add a player to an Activity. The first option is to use FlowplayerView.

The second option is to use either FlowplayerSupportFragment or FlowplayerFragment, depending on whether or not your Activity inherits (directly or indirectly) from AndroidX's FragmentActivity. This is the recommended way since these two Fragments wrap FlowplayerView and handle its lifecycle automatically for you. Optionally, both these Fragments can also handle orientation changes and toggle fullscreen. See Fullscreen for more information about fullscreen handling.

Add FlowplayerView

The FlowplayerView is the core of the Flowplayer Android SDK. It allows to 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 do not need this control then we recommend using either FlowplayerFragment or FlowplayerSupportFragment instead.

If you decide to use FlowplayerView instead of FlowplayerFragment or FlowplayerSupportFragment, then you need to take care of its lifecycle. There are two ways to achieve this:

  1. 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 its onCreate() method:
    FlowplayerLifecycleObserver.registerLifecycle(lifecycle)
  2. If your Activity or Fragment does not implement LifecycleOwner, then you need to call the FlowplayerView'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()
    }

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

Note that 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 player

FlowplayerView can play media either by fetching a so-called Flowplayer config or directly from a media URL that you specify.

In the first scenario, the player needs to be prepared with a Flowplayer mediaId and a playerId. This is achieved by preparing the player with a FlowplayerMedia instance, similar to the example below. The player will then fetch the Flowplayer config which contains the media URL, the ad schedule (if any), and branding-related information.

val flowplayerMedia = FlowplayerMedia("some-media-id", "some-player-id")
flowplayerView.prepare(flowplayerMedia, true)

In a similar way, 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. For more information about ads, see section Ads.

val externalMedia = ExternalMedia("https://link.to.a.media.file")
flowplayerView.prepare(externalMedia, true)
Results