Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Android: play /raw resource with Exoplayer
val player = ExoPlayerFactory.newSimpleInstance(context, DefaultTrackSelector())
val rawDataSource = RawResourceDataSource(context)
// open the /raw resource file
// create a media source with the raw DataSource
val mediaSource = ExtractorMediaSource.Factory(DataSource.Factory { rawDataSource })
// play immediately if true
player.playWhenReady = true
// setup the player using the source

This comment has been minimized.

Copy link

@sergchil sergchil commented Aug 21, 2019


val dataSourceFactory = DefaultDataSourceFactory(this, Util.getUserAgent(this, "ExoTest"))
val mediaSource = ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(rawDataSource.uri)

ExtractorMediaSource.Factory is depricated


This comment has been minimized.

Copy link

@yusufceylan yusufceylan commented Sep 15, 2020

For ExoPlayer 2.12 use like this

    private fun buildRawMediaSource(): MediaSource? {	
        val rawDataSource = RawResourceDataSource(this)	
        // open the /raw resource file	

        // Create media Item	
        val mediaItem = MediaItem.fromUri(rawDataSource.uri!!)	

        // create a media source with the raw DataSource	
        val mediaSource = ProgressiveMediaSource.Factory { rawDataSource }	

        return mediaSource	

For playing the resource

    private fun initializePlayer(){	
        exoPlayer = SimpleExoPlayer.Builder(this).build()	
        video_view.player = exoPlayer	

        val mediaSource = buildRawMediaSource(uri)	
        mediaSource?.let {	
            exoPlayer.playWhenReady = false	
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment