Skip to content

Instantly share code, notes, and snippets.

Embed
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
rawDataSource.open(DataSpec(RawResourceDataSource.buildRawResourceUri(R.raw.brown)))
// create a media source with the raw DataSource
val mediaSource = ExtractorMediaSource.Factory(DataSource.Factory { rawDataSource })
.createMediaSource(rawDataSource.uri)
// play immediately if true
player.playWhenReady = true
// setup the player using the source
player.prepare(mediaSource)
@sergchil

This comment has been minimized.

Copy link

@sergchil sergchil commented Aug 21, 2019

use

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

ExtractorMediaSource.Factory is depricated

@yusufceylan

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	
        rawDataSource.open(DataSpec(RawResourceDataSource.buildRawResourceUri(R.raw.brown)))	

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

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

        return mediaSource	
    }

For playing the resource

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

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