Skip to main content
Version: 3.x.x

Stop HLS - Android

Stopping HLS ends the ongoing HLS stream, so it is no longer available to viewers.

Stopping HLS​

  • Call stopHLS() on the Room to stop the HLS stream.
note
  • stopHLS() is a suspend function. Call it from a coroutine, such as lifecycleScope.launch (from androidx.lifecycle:lifecycle-runtime-ktx).
  • It returns once the request is sent. onHlsStateChanged reports HLSState.HLS_STOPPED when the stream has stopped.
  • If the request fails, it throws VideoSDKException.
  • Called before the room is joined, or while it is reconnecting, it does nothing and reports 3035 (ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINED) or 3065 (ERROR_ROOM_RECONNECTING) on onError instead of throwing.

Example​

findViewById<View>(R.id.btnStopHls).setOnClickListener { view: View? ->
lifecycleScope.launch {
try {
// Stop HLS
room!!.stopHLS()
} catch (e: VideoSDKException) {
Log.e("VideoSDK", "Stop HLS failed: ${e.code} ${e.name}")
}
}
}

Event associated with HLS​

  • onHlsStateChanged - Whenever the room's HLS state changes, the onHlsStateChanged event will trigger with an HLSState.

  • You will get the HLS_STOPPING and HLS_STOPPED states on calling stopHLS().

private val roomEventListener: RoomEventListener = object : RoomEventListener() {
override fun onHlsStateChanged(state: HLSState, hlsUrl: HLSUrl?) {
when (state) {
HLSState.HLS_STARTING -> Log.d("onHlsStateChanged", "Room hls is starting")
HLSState.HLS_STARTED -> Log.d("onHlsStateChanged", "Room hls is started")
HLSState.HLS_PLAYABLE -> {
Log.d("onHlsStateChanged", "Room hls is playable now")
// on hls playable you will receive the playback URL
val playbackHlsUrl = hlsUrl?.playbackHlsUrl
}
HLSState.HLS_STOPPING -> Log.d("onHlsStateChanged", "Room hls is stopping")
HLSState.HLS_STOPPED -> Log.d("onHlsStateChanged", "Room hls is stopped")
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
//...

// add listener to room
room!!.addEventListener(roomEventListener)
}

API Reference​

The API references for all the methods utilized in this guide are provided below.

Got a Question? Ask us on discord