Skip to content

Instantly share code, notes, and snippets.

@scruffyfox
Last active February 1, 2023 12:57
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scruffyfox/885cfbe77327d248a7e67f53b16bf079 to your computer and use it in GitHub Desktop.
Save scruffyfox/885cfbe77327d248a7e67f53b16bf079 to your computer and use it in GitHub Desktop.
Playing a video from Assets using ExoPlayer 2
/**
* It took me AGES to figure this out, seriously, exoplayer documentation IS SO BAD.
*/
package xxx
import android.net.Uri
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.cube.arc.R
import com.cube.lib.util.bind
import com.google.android.exoplayer2.ExoPlayerFactory
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory
import com.google.android.exoplayer2.source.ExtractorMediaSource
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
import com.google.android.exoplayer2.ui.SimpleExoPlayerView
import com.google.android.exoplayer2.upstream.AssetDataSource
import com.google.android.exoplayer2.upstream.DataSource
import com.google.android.exoplayer2.upstream.DataSource.Factory
class VideoPlayerActivity : AppCompatActivity()
{
lateinit var player: SimpleExoPlayer
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.video_player_view)
val videoPlayer = findViewById(R.id.video_player) as SimpleExoPlayerView
val videoTrackSelectionFactory = AdaptiveTrackSelection.Factory(null)
val trackSelector = DefaultTrackSelector(videoTrackSelectionFactory)
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector)
videoPlayer.requestFocus()
videoPlayer.useController = true
videoPlayer.player = player
val dataSourceFactory: DataSource.Factory = object : Factory
{
override fun createDataSource(): DataSource
{
return AssetDataSource(this@VideoPlayerActivity)
}
}
val videoSource = ExtractorMediaSource(Uri.parse("assets:///onboarding_video.mp4"), dataSourceFactory, DefaultExtractorsFactory(), null, null)
player.prepare(videoSource)
player.playWhenReady = true
}
override fun onDestroy()
{
super.onDestroy()
player.release()
}
}
@MangeshKadam
Copy link

you can change deprecated method code like below,
val videoSource = ExtractorMediaSource.Factory(factory).createMediaSource(Uri.parse("assets:///onboarding_video.mp4"))

@timxyz
Copy link

timxyz commented Aug 22, 2019

^ This kind soul has explained what the ExoPlayer docs couldn't

@mattiaferigutti
Copy link

mattiaferigutti commented Aug 20, 2020

Some of the libraries are now deprecated. I updated the code that you can find at this LINK if you need it

@dturner
Copy link

dturner commented Mar 22, 2021

Since this is the first result on Google for "ExoPlayer play local asset" I thought I share a slightly simpler way of playing local assets (thanks to andrewlewis@).

ExoPlayer 2.12 introduces the MediaItem class so you can do:

val firstVideoUri = Uri.parse("asset:///localfile.mp3")
val firstItem = MediaItem.fromUri(firstVideoUri)
player.addMediaItem(firstItem)

Note that the URI should start with asset:/// not assets:///

@grndvl1
Copy link

grndvl1 commented Jun 9, 2021

Since this is the first result on Google for "ExoPlayer play local asset" I thought I share a slightly simpler way of playing local assets (thanks to andrewlewis@).

ExoPlayer 2.12 introduces the MediaItem class so you can do:

val firstVideoUri = Uri.parse("asset:///localfile.mp3")
val firstItem = MediaItem.fromUri(firstVideoUri)
player.addMediaItem(firstItem)

Note that the URI should start with asset:/// not assets:///

This worked for me, I had assets not asset and I had 4 '/'. Funny thing is the old way worked with assets
ExtractorMediaSource(Uri.parse("assets:///test.mp4"), dataSourceFactory, DefaultExtractorsFactory(), null, null))

@pandelisgreen13
Copy link

Since this is the first result on Google for "ExoPlayer play local asset" I thought I share a slightly simpler way of playing local assets (thanks to andrewlewis@).

ExoPlayer 2.12 introduces the MediaItem class so you can do:

val firstVideoUri = Uri.parse("asset:///localfile.mp3")
val firstItem = MediaItem.fromUri(firstVideoUri)
player.addMediaItem(firstItem)

Note that the URI should start with asset:/// not assets:///

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment