Android 播放器 SDK

快速入門: BlendVision One 串流播放

(以下內容均翻譯自英文版文件,最新資訊及內容敬請切換至英文語系參考原文)

 

本指南將指導您如何通過 Android 播放器 SDK ,啟動 BlendVision One 串流播放的步驟。

 

此外,您還可以確認以下範例專案,以便更順利地開始工作:
https://github.com/BlendVision/Android-Player-SDK/tree/main/samples

 

開始之前

在實施步驟之前,請確保您已準備好 BlendVision One 的串流內容。您可以通過兩種方式準備您的內容:

步驟 1:取得播放令牌

  • 按照 驗證步驟 取得有效的 API 令牌
  • 取得預期要播放的串流內容的 resource_idresource_type
    有兩種方法可以取得此資訊:

  • 使用 API 取得 playback token/bv/cms/v1/tokens ,並將 resource_idresource_type 設為主體參數
    • 您也可以在主體參數中加入 customer_id  ,以使用自定義的使用者 ID 來追蹤使用者的播放行為
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
)

 

步驟 2:開始播放工作階段

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
)

 

步驟 3:初始化播放器

  • 為了播放串流內容,請在初始化配置中使用許可證密鑰和串流資訊建立播放器實例:
// create the player instance
private var player: UniPlayer? = null
player = UniPlayer.Builder(
        requireContext(),
        PlayerConfig(
            license = "YOUR_LICENSE_KEY"
        )
    ).build()
    
// set player view type
binding.kksPlayerServiceView.setupControlPanel(
    defaultContentType = ContentType.STANDALONE
)

// bind the new created player to player view
binding.playerView.setUnifiedPlayer(player)

// load stream information
player?.load(
    MediaConfig(
        source = listOf(
            MediaConfig.Source(
                url = streamInfoResponse.sources[0].manifests.filter { it.protocol == MediaConfig.Protocol.DASH }[0].url,
                protocal = MediaConfig.Protocol.DASH,
                drm = MediaConfig.DrmInfo.Widevine(
                    licenseUrl = startSessionResponse.endPoint,
                    headers = mapOf("" to "")
                )
            )
        ),
        title = "",
        imageUrl = "",
        thumbnailSeekingUrl = ""
    )
)

// start playback
player?.start()

// pause/play during playback
player?.pause()
player?.play()

// release the player
player?.release()

 

步驟 4:管理播放工作階段的生命週期

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

 

了解更多

更新於