Search in sources :

Example 11 with CacheDataSourceFactory

use of com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory in project Slide by ccrama.

the class ExoVideoView method setVideoURI.

/**
 * Sets the player's URI and prepares for playback
 *
 * @param uri      URI
 * @param type     Type of video
 * @param listener EventLister attached to the player, helpful for player state
 */
public void setVideoURI(Uri uri, VideoType type, Player.Listener listener) {
    // Create the data sources used to retrieve and cache the video
    DataSource.Factory downloader = new OkHttpDataSource.Factory(Reddit.client).setUserAgent(context.getString(R.string.app_name));
    DataSource.Factory cacheDataSourceFactory = new CacheDataSource.Factory().setCache(Reddit.videoCache).setUpstreamDataSourceFactory(downloader);
    // Create an appropriate media source for the video type
    MediaSource videoSource;
    switch(type) {
        // DASH video, e.g. v.redd.it video
        case DASH:
            videoSource = new DashMediaSource.Factory(cacheDataSourceFactory).createMediaSource(MediaItem.fromUri(uri));
            break;
        // Standard video, e.g. MP4 file
        case STANDARD:
        default:
            videoSource = new ProgressiveMediaSource.Factory(cacheDataSourceFactory).createMediaSource(MediaItem.fromUri(uri));
            break;
    }
    player.setMediaSource(videoSource);
    player.prepare();
    if (listener != null) {
        player.addListener(listener);
    }
}
Also used : ProgressiveMediaSource(com.google.android.exoplayer2.source.ProgressiveMediaSource) DashMediaSource(com.google.android.exoplayer2.source.dash.DashMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) OkHttpDataSource(com.google.android.exoplayer2.ext.okhttp.OkHttpDataSource) CacheDataSource(com.google.android.exoplayer2.upstream.cache.CacheDataSource) OkHttpDataSource(com.google.android.exoplayer2.ext.okhttp.OkHttpDataSource) DataSource(com.google.android.exoplayer2.upstream.DataSource)

Aggregations

CacheDataSource (com.google.android.exoplayer2.upstream.cache.CacheDataSource)9 DataSource (com.google.android.exoplayer2.upstream.DataSource)6 Test (org.junit.Test)5 Uri (android.net.Uri)4 StreamKey (com.google.android.exoplayer2.offline.StreamKey)4 MediaItem (com.google.android.exoplayer2.MediaItem)3 DefaultDownloaderFactory (com.google.android.exoplayer2.offline.DefaultDownloaderFactory)3 Downloader (com.google.android.exoplayer2.offline.Downloader)3 DownloaderFactory (com.google.android.exoplayer2.offline.DownloaderFactory)3 File (java.io.File)3 NotificationManager (android.app.NotificationManager)2 OkHttpDataSource (com.google.android.exoplayer2.ext.okhttp.OkHttpDataSource)2 DefaultExtractorsFactory (com.google.android.exoplayer2.extractor.DefaultExtractorsFactory)2 MediaSource (com.google.android.exoplayer2.source.MediaSource)2 DashMediaSource (com.google.android.exoplayer2.source.dash.DashMediaSource)2 AdaptationSet (com.google.android.exoplayer2.source.dash.manifest.AdaptationSet)2 DashManifest (com.google.android.exoplayer2.source.dash.manifest.DashManifest)2 Representation (com.google.android.exoplayer2.source.dash.manifest.Representation)2 FakeDataSet (com.google.android.exoplayer2.testutil.FakeDataSet)2 FakeDataSource (com.google.android.exoplayer2.testutil.FakeDataSource)2