Video Player
This guide will walk you through the steps to start a BlendVision One streaming playback using the Android player SDK.
Additionally, you can also check out the sample project for an even smoother start: https://github.com/BlendVision/Android-Player-Samples
Prerequisites
- A valid BlendVision Player License Key
Step 1: Initialize the Player
Import Player SDK
- Follow the integration to import SDK
- Add the latest version in the gradle
implementation "com.blendvision.player:playback:${player_version}"
implementation "com.blendvision.player:download:${player_version}"
implementation "com.blendvision.player:analytics:${player_version}"
implementation "com.blendvision.player:common:${player_version}"
Others (can be imported from public maven)
//[optional] Chromecast
implementation "com.google.android.gms:play-services-cast-framework:$cast_version"
Set gradle.properties
android.enableDexingArtifactTransform.desugaring=false
android.enableJetifier=true
Create a Player
To play the streaming content, create a player instance with the license key and stream information in the initialization configuration:
- Android
- Android TV
// create the player instance
private var player: UniPlayer? = null
player = UniPlayer.Builder(
requireContext(),
PlayerConfig(
license = "YOUR_LICENSE_KEY"
)
).build()
// set player view type
binding.playerView.setupControlPanel(
defaultContentType = ContentType.STANDALONE
)
// bind the new created player to player view
binding.playerView.setUnifiedPlayer(player)
// load stream information
player?.load(
MediaConfig(
source = MediaConfig.Source(
url = "DASH_MPD_URL",
protocal = MediaConfig.Protocol.DASH,
drm = MediaConfig.DrmInfo.Widevine(
licenseUrl = "DRM_SERVER_URL",
headers = mapOf("KEY" to "VALUE")
)
),
title = "",
imageUrl = "",
thumbnailSeekingUrl = ""
)
)
// start playback
player?.start()
// pause/play during playback
player?.pause()
player?.play()
// release the player
player?.release()
// create the player instance
private var player: UniPlayer? = null
player = UniPlayer.Builder(
requireContext(),
PlayerConfig(
license = "YOUR_LICENSE_KEY"
)
).build()
val uniTvFragment: UniTvFragment =
supportFragmentManager.findFragmentById(R.id.uniTvFragment) as UniTvFragment
player?.let {
uniTvFragment.setup(it)
}
// To pass events sent by the TV remote control,
// bypass the onKeyUp/onKeyDown events through the following interface.
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
return if (isFragmentHandleOnKeyUpEvent(keyCode, event)) {
// If isFragmentHandleOnKeyUpEvent return true, it means we handle the onKeyUp event.
// So we return true to tell parent we has handled it.
true
} else {
super.onKeyUp(keyCode, event)
}
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
return if (isFragmentHandleOnKeyDownEvent(keyCode, event)) {
// If isFragmentHandleOnKeyUpEvent return true, it means we handle the onKeyUp event.
// So we return true to tell parent we has handled it.
true
} else {
super.onKeyDown(keyCode, event)
}
}
private fun isFragmentHandleOnKeyUpEvent(keyCode: Int, event: KeyEvent?): Boolean {
return (uniTvFragment as? CustomUniTVFragmentInterface)?.onKeyUp(keyCode, event, null)
?: false
}
private fun isFragmentHandleOnKeyDownEvent(keyCode: Int, event: KeyEvent?): Boolean {
return (uniTvFragment as? CustomUniTVFragmentInterface)?.onKeyDown(keyCode, event, null)
?: false
}
// load stream information
player?.load(
MediaConfig(
source = MediaConfig.Source(
url = "DASH_MPD_URL",
protocal = MediaConfig.Protocol.DASH,
drm = MediaConfig.DrmInfo.Widevine(
licenseUrl = "DRM_SERVER_URL",
headers = mapOf("KEY" to "VALUE")
)
),
title = "",
imageUrl = "",
thumbnailSeekingUrl = ""
)
)
// start playback
player?.start()
// pause/play during playback
player?.pause()
player?.play()
// release the player
player?.release()
Step 2: Obtain a Playback Token
Make a POST request to the API: POST /bv/cms/v1/tokens
Query Parameters | Required | Description |
---|---|---|
resource_id | Required | The unique ID of your content. You can retrieve this in two ways: 1. Using BlendVision One CMS - VOD > Publishing Tools - Live > Publishing Tools 2. Using BlendVision One API - VOD: GET /bv/cms/v1/vods - Live: GET /bv/cms/v1/lives |
resource_type | Required | The streaming type of your content. |
customer_id | Optional | Your custom user ID to identify who is watching the content. This parameter is required if the content's security privacy is set to SECURITY_PRIVACY_TYPE_TOKEN . |
const val API_DOMAIN = "https://api.one.blendvision.com"
interface ApiService {
@POST("/bv/cms/v1/tokens")
suspend fun getPlaybackToken(
@Header("Authorization") cmsToken: String, // 'Bearer ${CMS token}'
@Body request: PlaybackTokenRequest()
}: PlaybackTokenResponse
}
data class PlaybackTokenRequest(
@SerializedName("resource_id")
val resourceId: String,
@SerializedName("resource_type")
val resourceType: String,
@SerializedName("customer_id")
val customerId: String
)
data class PlaybackTokenResponse(
@SerializedName("token")
val playbackToken: String
)
Step 3: Start a Playback Session
Start a playback session and obtain the stream data using the APIs:
Replace the sample URLs in Step 1 with the obtained manifest_url
(dashUrl
) for playback.
const val API_DOMAIN = "https://api.one.blendvision.com/"
interface ApiService {
@POST("bv/playback/v1/sessions/{deviceId}:start")
suspend fun startPlaybackSession(
@Header("Authorization") playbackToken: String,
@Path("deviceId") deviceId: String
): StartSessionResponse
@POST("bv/playback/v1/sessions/${deviceId}")
suspend fun getStreamInfo(
@Header("Authorization") playbackToken: String,
@Path("deviceId") deviceId: String
): GetStreamInfoResponse
}
// data for start a session
data class StartSessionResponse(
@SerializedName("drm_server_endpoint")
val endPoint: String
)
// data for get stream information
data class GetStreamInfoResponse(
@SerializedName("sources")
val sources: List
)
data class Source(
@SerializedName("manifests")
val manifests: List,
@SerializedName("thumbnail_seeking_url")
val thumbnailSeekingUrl: String
)
data class Manifest(
@SerializedName("protocol")
val protocal: String,
@SerializedName("url")
val url: String,
@SerializedName("resolutions")
val resolutions: List
)
data class Resolution(
@SerializedName("height")
val height: String,
@SerializedName("width")
val width: String
)
Step 4: Manage the Lifecycle of a Playback Session
To keep the playback session alive, periodically invoke the heartbeat API every 10 seconds:
POST /bv/playback/v1/sessions/:device_id:heartbeat
const val API_DOMAIN = "https://api.one.blendvision.com/"
interface ApiService {
// post this API every 10 seconds
@POST("bv/playback/v1/sessions/${deviceId}:heartbeat")
suspend fun Heartbeat(
@Header("Authorization") playbackToken: String,
@Path("deviceId") deviceId: String
)
@POST("bv/playback/v1/sessions/${deviceId}:end")
suspend fun EndPlaybackSession(
@Header("Authorization") playbackToken: String,
@Path("deviceId") deviceId: String
)
}