HLS WebView Player Documentation

JitPack Badge

HLSWebViewPlayer is a lightweight Android library for streaming HLS video using a WebView and hls.js. It provides a highly stable playback experience across Android versions, avoiding native media player inconsistencies while offering a clean control API and Jetpack Compose support.

Features

Setup

1. Add JitPack Repository

// settings.gradle.kts
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url = uri("https://jitpack.io") } // Add this
    }
}
    

2. Add Dependency

// build.gradle.kts (app module)
dependencies {
    implementation("com.github.yohannestz:HLSWebViewPlayer:1.0.0")
}
    

3. Permissions

<uses-permission android:name="android.permission.INTERNET" />
    

Usage

Jetpack Compose

Step 1: Create Controller

val controller = remember { DefaultHlsPlayerController() }

Step 2: Add Player Composable

HlsWebViewPlayer(
    modifier = Modifier.aspectRatio(16 / 9f),
    controller = controller
)

Step 3: Control Playback

val playStatus by controller.playStatus.collectAsState()

Button(onClick = {
    if (playStatus == PlayStatus.PLAY) {
        controller.pause()
    } else {
        controller.play("https://your-stream-url.m3u8")
    }
}) {
    Text(if (playStatus == PlayStatus.PLAY) "Pause" else "Play")
}

XML Layouts

Step 1: Add Player View

<com.github.yohannestz.hlswebviewplayer.HlsPlayerView
    android:id="@+id/hlsPlayerView"
    android:layout_width="match_parent"
    android:layout_height="250dp" />
    

Step 2: Use Controller in Activity

val playerView = findViewById(R.id.hlsPlayerView)
val controller = playerView.controller

controller.playStatus.onEach { status ->
    // Update your UI
}.launchIn(lifecycleScope)

controller.play("https://your-stream-url.m3u8")

Screenshots

Home Screen XML Sample Screen Compose Sample Screen


For source code and contributions, visit the GitHub repository: https://github.com/yohannestz/HLSWebViewPlayer