Android ExoPlayer

ExoPlayer is an open source project that is not part of the Android framework and is distributed separately from the Android SDK.

Android ExoPlayer

Android ExoPlayer

Download sample code .  GoogleExoPlayerSample 

Sample APK : https://github.com/datanapps/GoogleExoPlayerSample/blob/master/screens/app-debug.apk

 

ExoPlayer’s standard audio and video components are built on Android’s MediaCodec API, which was released in Android 4.1 (API level 16). Because ExoPlayer is a library, you can easily take advantage of new features as they become available by updating your app.

 

----------------------------------------------------------------

Creating a sample project in android studio

 

1. Create a sample project from android studio. to create a sample project please go to below steps:

A. File  >> New  >> New Project >> A popup will come then select an 'Empty Activity'.

B. Select Project name, package name, SDK Version and Language Kotlin.

 

2. Open project app -> res -> change resource values file like strings, color, dimens.

app > res > values > colors.xml  :

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
</resources>

app > res > values > styles.xml : 

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppFullScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

</resources>

app > res > values > string.xml :

<resources>
    <string name="app_name">Android Searchview</string>
</resources>

 

3. Now add dependencies in app gradle file. 

 

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.datanapps.googleexoplayersample"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
// exoplayer
    implementation 'com.google.android.exoplayer:exoplayer-core:2.9.4'
    implementation 'com.google.android.exoplayer:exoplayer-dash:2.8.2'
    implementation 'com.google.android.exoplayer:exoplayer-ui:2.8.2'
//
    implementation 'com.github.HaarigerHarald:android-youtubeExtractor:master-SNAPSHOT'
}

 

4. Create a layout for VideoActivity class :

app > res > layout > activity_video.xml 
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".VideoActivity">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/playerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

 

5.  Create VideoActivity to play video in layout :

app >> src >> main >> java >> com.datanapps.googleexoplayersample 

 

package com.datanapps.googleexoplayersample

import android.annotation.SuppressLint
import android.net.Uri
import android.os.Bundle
import android.util.SparseArray
import androidx.appcompat.app.AppCompatActivity
import at.huber.youtubeExtractor.VideoMeta
import at.huber.youtubeExtractor.YouTubeExtractor
import at.huber.youtubeExtractor.YtFile
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.ExtractorMediaSource
import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory
import kotlinx.android.synthetic.main.activity_video.*

class VideoActivity : AppCompatActivity() {

    lateinit var player : SimpleExoPlayer

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

    }

    private fun initializePlayer() {

        player = ExoPlayerFactory.newSimpleInstance(this, DefaultRenderersFactory(this),
            DefaultTrackSelector(), DefaultLoadControl()
        )

        playerView.setPlayer(player)

        player.setPlayWhenReady(true)
        player.seekTo(0, 0)

        // play MP4 Videos

       val uri = Uri.parse("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4")
        val mediaSource = buildMediaSource(uri)
        player.prepare(mediaSource, true, false)

// Play youtube video
//added dependency 
// implementation 'com.github.HaarigerHarald:android-youtubeExtractor:master-SNAPSHOT'   
// extractYoutubeUrl("https://www.youtube.com/watch?v=OJ3K90FpQ6A");

    }

    @SuppressLint("StaticFieldLeak")
    private fun extractYoutubeUrl(youtubeLink:String) {
        object : YouTubeExtractor(this) {
            public override fun onExtractionComplete(
                ytFiles: SparseArray<YtFile>?,
                vMeta: VideoMeta
            ) {
                if (ytFiles != null) {
                    val itag = 22
                    val downloadUrl = ytFiles.get(itag).url

                    // play url
                    val uri = Uri.parse(downloadUrl)
                    val mediaSource = buildMediaSource(uri)
                    player.prepare(mediaSource, true, false)

                }
            }
        }.extract(youtubeLink, true, true)

    }

    private fun buildMediaSource(uri: Uri): MediaSource {
        return ExtractorMediaSource.Factory(
            DefaultHttpDataSourceFactory("exoplayer-codelab")
        ).createMediaSource(uri)
    }

    public override fun onStart() {
        super.onStart()
        initializePlayer()
    }

    public override fun onPause() {
        super.onPause()
        releasePlayer()
    }

    public override fun onStop() {
        super.onStop()
        releasePlayer()
    }

    private fun releasePlayer() {
        if (playerView != null) {
            var playbackPosition = player.getCurrentPosition()
            var currentWindow = player.getCurrentWindowIndex()
            var playWhenReady = player.getPlayWhenReady()
            player.release()
            //player = null
        }
    }

}

 Hope it will help you.

 

Read More : https://codelabs.developers.google.com/codelabs/exoplayer-intro/#2 

-------------------------------------------------------------

Also, check out our Blogging for developers : 
Android Search View

But in the meantime, you can suggest me in comments for next ' Title '. please share your thoughts, tips with us, I'm sure you have a good points so please comment below or mail us at datanapps@gmail.com.

Please If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter or Facebook.

Thank you so much, Have a great day !

Next week I’ll post   Firebase Live Data  which is important for us in daily life.


Click Here To See More

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
1
wow
0