我有这个错误“java.lang.IllegalStateException:另一个SimpleCache实例使用文件夹:”我正在使用SimpleExoPlayer和此错误显示当我尝试第二次打开视频时如何关闭或删除以前的simplecache?这是我的代码:
SimpleExoPlayerView simpleExoPlayerView = findViewById(R.id.video_view);
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector(new DefaultBandwidthMeter.Builder().build()));
SimpleCache downloadCache = new SimpleCache(new File(getCacheDir(), "exoCache"), new NoOpCacheEvictor());
String uri = "http://dash.akamaized.net/akamai/bbb/bbb_1280x720_60fps_6000k.mp4";
DataSource.Factory dataSourceFactory = new CacheDataSourceFactory(downloadCache, new DefaultDataSourceFactory(this, "seyed"));
MediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(uri));
player.prepare(mediaSource);
simpleExoPlayerView.setPlayer(player);
player.setPlayWhenReady(true);发布于 2018-09-26 08:05:27
您需要将缓存类设置为Singleton,以确保在所有应用程序中都有一个SimpleCache实例:
public class VideoCache {
private static SimpleCache sDownloadCache;
public static SimpleCache getInstance(Context context) {
if (sDownloadCache == null) sDownloadCache = new SimpleCache(new File(context.getCacheDir(), "exoCache"), new NoOpCacheEvictor(), new ExoDatabaseProvider(context));
return sDownloadCache;
}
}并在您的代码中使用它,如下所示:
DataSource.Factory dataSourceFactory = new CacheDataSourceFactory(VideoCache.getInstance(this), new DefaultDataSourceFactory(this, "seyed"));发布于 2019-11-19 19:15:49
将cache对象设为单例并不一定能解决问题,因为cache仍然可能由于先前的音频播放器调用而被锁定。当发生这种情况时,IllegalStateException仍然会被抛出。
为了解决这个问题,你需要在释放播放器的时候释放cache,也就是把这个放在播放器被覆盖的release方法中:
cache.release();
cache = null;https://stackoverflow.com/questions/52507270
复制相似问题