2015-02-24 215 views
18

我正在尋找在ExoPlayer中實現緩存的任何示例。在ExoPlayer中使用緩存

ExoPlayer在其庫中有關於緩存的不同類,Google在此video中解釋了我們可以使用CacheDataSource類實現它,但Google不提供任何演示。不幸的是,這看起來很複雜,所以我現在正在尋找例子(在Google上沒有成功)。

有沒有人成功或有任何信息可以幫助?謝謝。

+0

閱讀此文檔http://developer.android.com/guide/topics/media/exoplayer.html – rogerwar 2015-02-25 15:32:30

+0

很明顯,我讀了它......沒有顯然它是不可能實現它。這太糟糕了...... https://github.com/google/ExoPlayer/issues/57 – ilansas 2015-02-26 08:43:18

+0

分享你的代碼到目前爲止你做了什麼 – rogerwar 2015-02-26 10:30:36

回答

2

我在渲染建設者

private static final int BUFFER_SEGMENT_SIZE = 64 * 1024; 
private static final int BUFFER_SEGMENT_COUNT = 160; 

final String userAgent = Util.getUserAgent(mContext, appName); 
final DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); 
final Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);* 

Cache cache = new SimpleCache(context.getCacheDir(), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 10)); 
DataSource dataSource = new DefaultUriDataSource(context, bandwidthMeter, userAgent); 
CacheDataSource cacheDataSource = new CacheDataSource(cache, dataSource, false, false); 
ExtractorSampleSource sampleSource = new ExtractorSampleSource(uri 
       , cacheDataSource 
       , allocator 
       , BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE 
       , new Mp4Extractor()); 
+1

此代碼編譯並運行,但似乎沒有在指定的緩存文件夾中寫入任何視頻。它對你有用嗎?它是否從沒有互聯網連接的緩存播放?更深的信息,將不勝感激。謝謝 – voghDev 2015-10-05 11:15:24

+0

也添加了這段代碼,但如上所述,它看起來並不像緩存任何東西。有什麼我們在這裏失蹤? – Gil 2015-10-26 07:49:57

+2

根據https://github.com/google/ExoPlayer/issues/420,此答案僅適用於DASH流。對於MP4文件,OkHttpDataSource似乎會產生良好的效果(根據該線程中的人員)。 – 2016-04-22 10:15:31

10

這裏是ExoPlayer 2 +

創建自定義緩存數據源廠

class CacheDataSourceFactory implements DataSource.Factory { 
    private final Context context; 
    private final DefaultDataSourceFactory defaultDatasourceFactory; 
    private final long maxFileSize, maxCacheSize; 

    CacheDataSourceFactory(Context context, long maxCacheSize, long maxFileSize) { 
     super(); 
     this.context = context; 
     this.maxCacheSize = maxCacheSize; 
     this.maxFileSize = maxFileSize; 
     String userAgent = Util.getUserAgent(context, context.getString(R.string.app_name)); 
     DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); 
     defaultDatasourceFactory = new DefaultDataSourceFactory(this.context, 
       bandwidthMeter, 
       new DefaultHttpDataSourceFactory(userAgent, bandwidthMeter)); 
    } 

    @Override 
    public DataSource createDataSource() { 
     LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize); 
     SimpleCache simpleCache = new SimpleCache(new File(context.getCacheDir(), "media"), evictor); 
     return new CacheDataSource(simpleCache, defaultDatasourceFactory.createDataSource(), 
       new FileDataSource(), new CacheDataSink(simpleCache, maxFileSize), 
       CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, null); 
    } 
} 

而且選手

BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); 
TrackSelection.Factory videoTrackSelectionFactory = 
     new AdaptiveTrackSelection.Factory(bandwidthMeter); 
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); 

SimpleExoPlayer exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector); 
MediaSource audioSource = new ExtractorMediaSource(Uri.parse(url), 
      new CacheDataSourceFactory(context, 100 * 1024 * 1024, 5 * 1024 * 1024), new DefaultExtractorsFactory(), null, null); 
exoPlayer.setPlayWhenReady(true); 
exoPlayer.prepare(audioSource); 

它工作得很好的解決方案。

+0

我對這個解決方案的問題是,如果我使用它來緩存和播放多個視頻,多個播放器可能會播放相同的數據(即似乎對於不同的uri,從緩存中返回相同的數據流)。使用默認ExtractorMediaSource時不會發生此問題。 uri的視頻是獨一無二的 – 2017-09-20 12:09:59

+2

解決問題的方法:保留SimpleCache的共享實例,而不是在createDataSource中創建它。否則多個緩存對象將寫入相同的文件造成麻煩 – 2017-09-20 15:14:31

+0

其作品感謝 – 2017-09-26 08:15:05