DummyDataSource不能打开:Android ExoPlayer



我正在尝试从url流视频并在同一时间将它们保存在缓存中。

我正在使用Android Studio的DataBaseInspector来检查缓存,它似乎可以工作,但不是所有的时间。

当我尝试播放视频时,有时会出现这个错误:

2021-01-21 10:27:12.956 26648-28069/test.com.test E/ExoPlayerImplInternal: Playback error
com.google.android.exoplayer2.ExoPlaybackException: Source error
at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:554)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:238)
at android.os.HandlerThread.run(HandlerThread.java:67)
Caused by: java.io.IOException: DummyDataSource cannot be opened
at com.google.android.exoplayer2.upstream.DummyDataSource.open(DummyDataSource.java:39)
at com.google.android.exoplayer2.upstream.cache.CacheDataSource.openNextSource(CacheDataSource.java:764)
at com.google.android.exoplayer2.upstream.cache.CacheDataSource.open(CacheDataSource.java:579)
at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:84)
at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:1017)
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:415)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)

阅读错误,我们可以看到一个IOException,所以我已经检查了缓存中的路径,再次工作有时…我在exoplayer内部数据库中注册的键是url(我让exoplayer做所有的工作,但我检查了自己之后)。

下面是我的代码:
@Inject
public VideoCacheManager(@NonNull SimpleCache simpleCache, @ActivityContext Context context) {
this.simpleCache = simpleCache;
this.context = context;
cacheDataSource = new CacheDataSource.Factory();
cacheDataSource.setCache(simpleCache);
Log.d(TAG, "VideoCacheManager: cache size left : " + simpleCache.getCacheSpace());
cacheDataSource.setEventListener(new CacheDataSource.EventListener() {
@Override
public void onCachedBytesRead(long cacheSizeBytes, long cachedBytesRead) {
Log.d(TAG, "onCachedBytesRead: cached bytes read : " + cachedBytesRead);
}
@Override
public void onCacheIgnored(int reason) {
Log.d(TAG, "onCacheIgnored: cache ignored : " + reason);
}
});
}
public MediaSource buildMediaSource(MediaItem mediaItem) {
return new ProgressiveMediaSource.Factory(cacheDataSource).createMediaSource(mediaItem);
}

而SimpleCache是一个注入了Hilt的Singleton:

@Provides
@Singleton
public static SimpleCache simpleCache(@ApplicationContext Context context) {
return new SimpleCache(context.getApplicationContext().getCacheDir(), leastRecentlyUsedCacheEvictor(), exoDatabaseProvider(context));
}
@Provides
@Singleton
public static LeastRecentlyUsedCacheEvictor leastRecentlyUsedCacheEvictor() {
return new LeastRecentlyUsedCacheEvictor(exoPlayerCacheSize());
}
@Provides
@Singleton
public static Long exoPlayerCacheSize() {
return (long) (500 * 1024 * 1024);
}
@Provides
@Singleton
public static ExoDatabaseProvider exoDatabaseProvider(@ApplicationContext Context context) {
return new ExoDatabaseProvider(context);
}

在我的ExoPlayer类我有一个简单的方法:

public void prepare(MediaItem mediaItem) {
lastMediaItem = mediaItem;
Log.d(TAG, "prepare: media item : " + mediaItem.playbackProperties);
MediaSource mediaSource = videoCacheManager.buildMediaSource(mediaItem);
Log.d(TAG, "prepare: source : " + mediaSource.getInitialTimeline());
simpleExoPlayer.prepare(mediaSource);
}

我已经读了足够多的关于exoplayer和它应该做下载到缓存的视频,如果它不存在,在第一次流媒体会话期间…那么这个错误是什么以及如何修复它呢?

我第一次使用这个代码时它工作了,然后我清除了所有内容并重试,但没有成功。

好吧,我一定是错误地删除了DefaultDataSourceFactory,忘记将UpstreamDataSource设置回CacheDataSource实例。

现在一切都好了,希望这对其他人有帮助。

在代码中我们需要添加这个:

DefaultDataSourceFactory defaultDataSourceFactory = new DefaultDataSourceFactory(context, Util.getUserAgent(context, context.getString(R.string.app_name)));
cacheDataSource.setUpstreamDataSourceFactory(defaultDataSourceFactory);

看这个视频我也学到了很多关于ExoPlayer的知识,尽管它是在2018年的exoplayer 2.8版本。

我使用的是最新版本,即2.12.3.

最新更新