API
What you learn​
You're instantiating DIVA Player in your app and relying on DIVA Back Office as the video streaming source.
The goal of this article is to build the simplest front-end to stream a video from the DIVA Back Office and call API endpoints to play and pause the video.
Before starting​
- Ensure the DIVA Back Office instance you rely on is up and running.
- Ask your video engineers team
<videoId>
,<settingsURL>
,<languageCountryCode>
,<userToken>
and<sharedKey>
.
Note: Don't use API to alter the video player or video streaming behaviors when instantiating DIVA Player. API endpoints are designed to allow the front-end to control the video player or video streaming behaviors after DIVA player has been initialized.
Instantiation​
Use the api
property of the DivaFragment
instance to access API object, which is the on the success callback of the DivaBoAdapter.createDivaFragment()
method.
Use the following methods, callbacks, and event - ignore the others - and find their documentation inline:
general
:- methods:
- setTimelineIconsMinDistance
- callbacks:
- onTimelineMinDistanceReceived
- methods:
player
:- methods:
- sendPlayerCommand
- callbacks:
- onPlaybackSessionChanged
- onPlayerActionRequest
- onPlayerDurationChanged
- onPlayerPlaybackRateChanged
- onPlayerPositionChanged
- onPlayerStateChanged
- onUserLiveChanged
- onVideoShownChanged
- onVolumeChanged
- methods:
video
:- methods:
- none
- callbacks:
- onVideoEnded
- onVideoMetadataReceived
- methods:
watchTogether
(mobile only):- methods:
- setLauncherButton
- callbacks:
- none
- events:
- buttonVisibilityChange
- methods:
Working sample code​
The following working code mutes the video once started:
package com.example.divaplayerhelloworld20
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.deltatre.divaboadapter.DivaBoAdapter
import com.deltatre.divaboadapter.DivaExtraParams
import androidx.lifecycle.lifecycleScope
import com.deltatre.divacorelib.api.DivaListener
import com.deltatre.divacorelib.api.model.CustomActionPayload
import com.deltatre.divacorelib.api.player.PlayerAction
import com.deltatre.divacorelib.api.player.PlayerCallback
import com.deltatre.divacorelib.api.player.PlayerCommands
import com.deltatre.divacorelib.api.player.PlayerPosition
import com.deltatre.divacorelib.api.video.VideoCallback
import com.deltatre.divacorelib.data.analytics.AnalyticsEvent
import com.deltatre.divacorelib.exceptions.VideoError
import com.deltatre.divacorelib.models.AssetState
import com.deltatre.divacorelib.models.State
import com.deltatre.divacorelib.models.VideoMetadata
import com.deltatre.divacorelib.models.VideoMetadataClean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Date
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
val divaExtraParams = DivaExtraParams()
val boAdapter = DivaBoAdapter(this /*refers to the Activity class */, lifecycleScope)
boAdapter.createDivaFragment(
adapterSettingsUrl = "https://divademo.deltatre.net/DIVAProduct/www/Data/Diva5.xTest/settings/dyn/android.json",
langCountryCode = "en-US",
videoId = "bae2c2aa-0f4e-4113-8f33-ce41b6267cc9",
getCurrentUserAccountAccessToken = {
//Get the user token
""
},
sharedKey = "testpassword",
conf = divaExtraParams,
onError = { exception ->
//On Error
}
)
{ divaFragment ->
supportFragmentManager
.beginTransaction()
.replace(R.id.fragment_container_view, divaFragment)
.commit()
divaFragment.api.player.registerListener(object:PlayerCallback{
override fun onPlaybackSessionChanged(playbackSessionId: String) {
}
override fun onPlayerActionRequest(action: PlayerAction) {
}
override fun onPlayerDurationChanged(duration: PlayerPosition) {
}
override fun onPlayerPlaybackRateChanged(rate: Float) {
}
override fun onPlayerPositionChanged(position: PlayerPosition) {
}
override fun onPlayerStateChanged(state: State) {
}
override fun onUserLiveChanged(inLiveWindow: Boolean) {
}
override fun onVideoShownChanged(videoShown: Boolean) {
divaFragment.api.player.sendPlayerCommand(PlayerCommands.mute(true))
}
override fun onVolumeChanged(volume: Float) {
}
})}
}
}