Skip to main content
Version: Android SDK v1.3.0

Implementations

We provide a few Player implementations that you can choose from depending on your case. Using them is a bit different but they all share the same functionality from the Player interface.

  • A default implementation called DefaultPlayer
  • A few UI-specific implementations. These can be found in the io.video:videokit-player-ui module and generally implement Player by delegation to some DefaultPlayer under the hood.

The default player

DefaultPlayer is the low level implementation that, unlike the others, is detached from the UI. You will typically hold the default instance in the "view model" layer of your app, but you are free to do otherwise.

The player offers many constructors that should fit your use case:

  • DefaultPlayer(Context, SavedStateHandle?): to be used from AAC ViewModels, which can receive a SavedStateHandle into their constructor.
  • DefaultPlayer(ComponentActivity): to be used from activities.
  • DefaultPlayer(Fragment): to be used from fragments.
  • DefaultPlayer(Context, LifecycleOwner?, SavedStateRegistry, ActivityResultCaller): lower level constructor.

For example, in the view model scenario:

class RecordViewModel(app: Application, state: SavedStateHandle) : AndroidViewModel(app) {

val player = DefaultPlayer(app, state)

override fun onCleared() {
super.onCleared()
player.destroy()
}
}

As you can see, the DefaultPlayer must be destroyed when you're done with it.

Showing the UI

The default player creates and caches an android.view.View with the content to be shown (camera and video preview). In order to show the UI in your app, make sure to add this view to some parent view in your hierarchy:

val parentView: ViewGroup = findViewById(R.id.player_container)
parentView.addView(player.view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)

videokit-player-ui module: the player fragment

The PlayerFragment is a fragment implemented as described above, hosting the player in a view model. It is slightly easier to use since there is no need to release or care about the UI.

You can customize the fragment after it is attached or when creating it, thanks to the PlayerFragment.Options class:

val options = PlayerFragment.Options.build {
aspectMode(AspectMode.Crop)
playbackQuality(PlaybackQuality.Auto)
mute(false)
loop(true)
overlay(R.layout.player_options_overlay)
}
val fragment = PlayerFragment.newInstance(options)

videokit-player-ui module: the player view

The PlayerView is a view that holds a default player, to be used for codebases that do not use fragments at all. Like the fragment, this is a very simple option, although it has the drawback that the player state will not be kept across activity restoration.

videokit-player-ui module: Jetpack Compose player

For Jetpack Compose users, we provide two useful functions.

First, rememberPlayer: use this to efficiently create a DefaultPlayer and scope it to the composition. Under the hood, we take care about state restoration and lifecycle management.

Lastly, a Player() composable which accepts the player from above:

@Composable
fun PlayerScreen(modifier: Modifier = Modifier) {
val player = rememberPlayer()
Box(modifier) {
Player(player, Modifier.fillMaxSize())
// Overlays here...
}
}

Note that, despite the reactive composable, management of the player APIs must be done through the imperative APIs of DefaultPlayer. In fact, the default player is better hosted somewhere in the view model layer for ease of access and management.