Code Example

Kotlin Android HLS Player Snippets

A step by step Android HLS Player example.

1. TakuSemba/HlsPlayer

HLS Player on Android.

Set your hls url in local.properties

ndk.dir=/Users/takusemba/Library/Android/sdk/ndk-bundle
sdk.dir=/Users/takusemba/Library/Android/sdk

streaming_url = your hls url

Full Example

Let us look at a full Example below.

Step 1. Dependencies

We need to add some dependencies in our app/build.gradle file as shown below:

(a). build.gradle

dependencies {
//...

    implementation 'com.google.android.exoplayer:exoplayer-core:2.6.0'
    implementation 'com.google.android.exoplayer:exoplayer-hls:2.6.0'
    implementation 'com.google.android.exoplayer:exoplayer-ui:2.6.0'
}

def getStreamingUrl() {
    Properties properties = new Properties()
    properties.load(project.rootProject.file('local.properties').newDataInputStream())
    return properties.getProperty("streaming_url")
}

Step 2. Add Permissions

We will need to add some permissions in our AndroidManifest.xml as shown below:

(a). AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.takusemba.hlsplayer">

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

</manifest>

Step 3. Design Layouts

We need to design our XML layouts as follows:

(a). activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:resize_mode="fill"
        app:surface_type="texture_view"
        app:use_controller="false" />

    <Button
        android:id="@+id/retry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="true"
        android:text="@string/retry"/>

</android.support.constraint.ConstraintLayout>

Step 4. Write Code

Finally we need to write our code as follows:

(a). MainActivity.kt

package com.takusemba.hlsplayer

import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import com.google.android.exoplayer2.DefaultLoadControl
import com.google.android.exoplayer2.DefaultRenderersFactory
import com.google.android.exoplayer2.ExoPlayerFactory
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.source.hls.HlsMediaSource
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
import com.google.android.exoplayer2.ui.SimpleExoPlayerView
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory

class MainActivity : AppCompatActivity() {

    private val handler = Handler()
    private var player: SimpleExoPlayer? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        play()

        findViewById<Button>(R.id.retry).setOnClickListener {
            retry()
        }
    }

    private fun play() {
        player = ExoPlayerFactory.newSimpleInstance(
                DefaultRenderersFactory(this), DefaultTrackSelector(), DefaultLoadControl())
        val playerView = findViewById<SimpleExoPlayerView>(R.id.player_view)
        val uri = Uri.parse(BuildConfig.STREAMING_URL)

        playerView.player = player

        val dataSourceFactory = DefaultDataSourceFactory(this, "user-agent")
        val mediaSource = HlsMediaSource(uri, dataSourceFactory, handler, null)

        player?.prepare(mediaSource)
        player?.playWhenReady = true
    }

    private fun stop() {
        player?.playWhenReady = false
        player?.release()
    }

    private fun retry() {
        stop()
        play()
    }
}

Reference

You can DOWNLOAD FULL CODE.
You can also browse code or read more here.
Follow code author here.


Read More.