Site icon C1CTech

Android ExoPlayer Example in Kotlin

&NewLine;<p>This post is based on implementing Exoplayer Library to play media files on Android&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-buttons is-content-justification-center is-layout-flex wp-block-buttons-is-layout-flex">&NewLine;<div class&equals;"wp-block-button"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;ExoPlayerDemo" style&equals;"background-color&colon;&num;210e61" target&equals;"&lowbar;blank" rel&equals;"noreferrer noopener"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div>&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class&equals;"wp-block-embed&lowbar;&lowbar;wrapper">&NewLine;<span class&equals;"embed-youtube" style&equals;"text-align&colon;center&semi; display&colon; block&semi;"><amp-youtube data-videoid&equals;"CEWGCvjEnmA" data-param-rel&equals;"1" data-param-showsearch&equals;"0" data-param-showinfo&equals;"1" data-param-iv&lowbar;load&lowbar;policy&equals;"1" data-param-fs&equals;"1" data-param-hl&equals;"en-US" data-param-autohide&equals;"2" data-param-wmode&equals;"transparent" width&equals;"1200" height&equals;"675" layout&equals;"responsive"><a href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;CEWGCvjEnmA" placeholder><amp-img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;CEWGCvjEnmA&sol;hqdefault&period;jpg" alt&equals;"YouTube Poster" layout&equals;"fill" object-fit&equals;"cover"><noscript><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;CEWGCvjEnmA&sol;hqdefault&period;jpg" loading&equals;"lazy" decoding&equals;"async" alt&equals;"YouTube Poster"><&sol;noscript><&sol;amp-img><&sol;a><&sol;amp-youtube><&sol;span>&NewLine;<&sol;div><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon; &num;000080&semi;">ExoPlayer<&sol;span><&sol;strong>&nbsp&semi;<&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><span style&equals;"color&colon; &num;008000&semi;"><strong> ExoPlayer<&sol;strong><&sol;span> is an open source library maintained by Google&period;<&sol;li><li>It&nbsp&semi; supports a wide range of media files including DASH and HLS streaming which is not supported by the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>MediaPlayer<&sol;strong><&sol;span>&period;<&sol;li><li>It provides you the ability to customize your media player according to your requirements&period;<&sol;li><li>ExoPlayer provides the support for the playlist and also provides smooth encryption and streaming of video and audio files&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<p>Let&&num;8217&semi;s create a simple video player app in which we will be fetching a video from a URL and play that video inside our ExoPlayer&period;&nbsp&semi;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon; &num;000080&semi;">Creating new Project<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>1 &period; Create a new project by going to&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><b>File&nbsp&semi;<&sol;b><span class&equals;"s1"><b>&Implies;<&sol;b><&sol;span><&sol;span><b><span style&equals;"color&colon; &num;008000&semi;">&nbsp&semi;New Android Project<&sol;span>&comma;<&sol;b>&nbsp&semi;select&nbsp&semi;<span style&equals;"color&colon; &num;339966&semi;"><strong>Empty Activity<&sol;strong><&sol;span>&comma; provide&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>app name<&sol;strong><&sol;span>&comma;&nbsp&semi;select language to&nbsp&semi;<strong><span style&equals;"color&colon; &num;008000&semi;">kotlin<&sol;span>&nbsp&semi;<&sol;strong>and then finally click on&nbsp&semi;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>finish<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>2 &period; Open app-level&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;gradle<&sol;strong><&sol;span>&nbsp&semi;file&comma; add the dependency of<span style&equals;"color&colon; &num;008000&semi;"><strong> ExoPlayer<&sol;strong><&sol;span>&nbsp&semi;in the dependencies section&comma; and&nbsp&semi;<span style&equals;"color&colon; &num;000080&semi;"><strong>sync<&sol;strong><&sol;span>&nbsp&semi;the project&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>build&period;gradle<&sol;strong><&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">dependencies &lbrace;<br> <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;ExoPlayer dependency<&sol;span><&sol;strong><br> implementation 'com&period;google&period;android&period;exoplayer&colon;exoplayer&colon;2&period;15&period;0'<br>&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>3&period; Go to <strong><span style&equals;"color&colon; &num;008000&semi;">AndroidManifest&period;xml<&sol;span><&sol;strong> and add the internet permission above <span style&equals;"color&colon; &num;000080&semi;"><strong>&lt&semi;application&gt&semi;<&sol;strong><&sol;span> tag as shown below&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">AndroidManifest&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;uses-permission android&colon;name&equals;"android&period;permission&period;INTERNET"&sol;&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>4&period; Open the layout file <strong><span style&equals;"color&colon; &num;008000&semi;">activity&lowbar;main&period;xml <&sol;span><&sol;strong>and add the <strong><span style&equals;"color&colon; &num;000080&semi;">PlayerView<&sol;span><&sol;strong> as shown below&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;main&period;xml<&sol;span> <&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br>&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br> xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br> xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<br> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&gt&semi;<br><br> &lt&semi;com&period;google&period;android&period;exoplayer2&period;ui&period;PlayerView<br> android&colon;id&equals;"&commat;&plus;id&sol;playerView"<br> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br> android&colon;layout&lowbar;height&equals;"match&lowbar;parent" &sol;&gt&semi;<br><br>&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>5&period; Open <strong><span style&equals;"color&colon; &num;008000&semi;">MainActivity&period;kt<&sol;span><&sol;strong> and add the below code&colon;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;kt<&sol;strong><&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">package com&period;c1ctech&period;exoplayerdemo<br><br>import androidx&period;appcompat&period;app&period;AppCompatActivity<br>import android&period;os&period;Bundle<br>import com&period;google&period;android&period;exoplayer2&period;&ast;<br>import com&period;google&period;android&period;exoplayer2&period;source&period;MediaSource<br>import com&period;google&period;android&period;exoplayer2&period;ui&period;PlayerView<br>import com&period;google&period;android&period;exoplayer2&period;util&period;Util<br>import com&period;google&period;android&period;exoplayer2&period;source&period;ProgressiveMediaSource<br>import com&period;google&period;android&period;exoplayer2&period;upstream&period;&ast;<br>import com&period;google&period;android&period;exoplayer2&period;upstream&period;DefaultHttpDataSource<br><br>class MainActivity &colon; AppCompatActivity&lpar;&rpar;&comma; Player&period;Listener &lbrace;<br><br> private var mPlayer&colon; SimpleExoPlayer&quest; &equals; null<br> private lateinit var playerView&colon; PlayerView<br><br> private val videoURL &equals;<br> "https&colon;&sol;&sol;commondatastorage&period;googleapis&period;com&sol;gtv-videos-bucket&sol;sample&sol;BigBuckBunny&period;mp4"<br><br> override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<br> super&period;onCreate&lpar;savedInstanceState&rpar;<br> setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;<br><br> <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;get PlayerView by its id<&sol;span><&sol;strong><br> playerView &equals; findViewById&lpar;R&period;id&period;playerView&rpar;<br><br> &rcub;<br><br> private fun initPlayer&lpar;&rpar; &lbrace;<br><br><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; Create a player instance&period;<&sol;strong><&sol;span><br> mPlayer &equals; SimpleExoPlayer&period;Builder&lpar;this&rpar;&period;build&lpar;&rpar;<br><br><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; Bind the player to the view&period;<&sol;strong><&sol;span><br> playerView&period;player &equals; mPlayer<br><br><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;setting exoplayer when it is ready&period;<&sol;strong><&sol;span><br> mPlayer&excl;&excl;&period;playWhenReady &equals; true<br><br><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; Set the media source to be played&period;<&sol;span><&sol;strong><br> mPlayer&excl;&excl;&period;setMediaSource&lpar;buildMediaSource&lpar;&rpar;&rpar;<br><br><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; Prepare the player&period;<&sol;span><&sol;strong><br> mPlayer&excl;&excl;&period;prepare&lpar;&rpar;<br><br> &rcub;<br><br> override fun onStart&lpar;&rpar; &lbrace;<br> super&period;onStart&lpar;&rpar;<br> if &lpar;Util&period;SDK&lowbar;INT &gt&semi;&equals; 24&rpar; &lbrace;<br> initPlayer&lpar;&rpar;<br> &rcub;<br> &rcub;<br><br> override fun onResume&lpar;&rpar; &lbrace;<br> super&period;onResume&lpar;&rpar;<br> if &lpar;Util&period;SDK&lowbar;INT &lt&semi; 24 &vert;&vert; mPlayer &equals;&equals; null&rpar; &lbrace;<br> initPlayer&lpar;&rpar;<br> &rcub;<br> &rcub;<br><br> override fun onPause&lpar;&rpar; &lbrace;<br> super&period;onPause&lpar;&rpar;<br> if &lpar;Util&period;SDK&lowbar;INT &lt&semi; 24&rpar; &lbrace;<br> releasePlayer&lpar;&rpar;<br> &rcub;<br> &rcub;<br><br> override fun onStop&lpar;&rpar; &lbrace;<br> super&period;onStop&lpar;&rpar;<br> if &lpar;Util&period;SDK&lowbar;INT &gt&semi;&equals; 24&rpar; &lbrace;<br> releasePlayer&lpar;&rpar;<br> &rcub;<br> &rcub;<br><br><br> private fun releasePlayer&lpar;&rpar; &lbrace;<br> if &lpar;mPlayer &equals;&equals; null&rpar; &lbrace;<br> return<br> &rcub;<br><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;release player when done<&sol;span><&sol;strong><br> mPlayer&excl;&excl;&period;release&lpar;&rpar;<br> mPlayer &equals; null<br> &rcub;<br><br><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;creating mediaSource<&sol;span><&sol;strong><br> private fun buildMediaSource&lpar;&rpar;&colon; MediaSource &lbrace;<br><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; Create a data source factory&period;<&sol;span><&sol;strong><br> val dataSourceFactory&colon; DataSource&period;Factory &equals; DefaultHttpDataSource&period;Factory&lpar;&rpar;<br><br><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; Create a progressive media source pointing to a stream uri&period;<&sol;span><&sol;strong><br> val mediaSource&colon; MediaSource &equals; ProgressiveMediaSource&period;Factory&lpar;dataSourceFactory&rpar;<br> &period;createMediaSource&lpar;MediaItem&period;fromUri&lpar;videoURL&rpar;&rpar;<br><br> return mediaSource<br> &rcub;<br>&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>In the above code&comma; getting started with <span style&equals;"color&colon; &num;008000&semi;"><strong>ExoPlayer<&sol;strong><&sol;span>&nbsp&semi;consists of implementing the following steps&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ol class&equals;"wp-block-list"><li>Create a <span style&equals;"color&colon; &num;008000&semi;"><strong>SimpleExoPlayer<&sol;strong><&sol;span>&nbsp&semi;instance&period;<&sol;li><li>Attach the player to a <strong><span style&equals;"color&colon; &num;008000&semi;">PlayerView<&sol;span><&sol;strong>&period;<&sol;li><li>Set player with <strong><span style&equals;"color&colon; &num;008000&semi;">MediaSource<&sol;span><&sol;strong> and then <span style&equals;"color&colon; &num;008000&semi;"><strong>prepare<&sol;strong><&sol;span> the player&period;<&sol;li><li><strong><span style&equals;"color&colon; &num;008000&semi;">Release<&sol;span><&sol;strong> the player when done&period;<&sol;li><&sol;ol>&NewLine;&NewLine;&NewLine;&NewLine;<p>6&period; When you run the app it will look like this as shown below&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-image"><figure class&equals;"aligncenter"><img src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;09&sol;Screenshot&lowbar;20210923-155316&lowbar;ExoPlayerDemo-498x1024&period;jpg" alt&equals;"" class&equals;"wp-image-2790"&sol;><&sol;figure><&sol;div>&NewLine;&NewLine;

Exit mobile version