java.lang.IllegalStateException:另一个 SimpleCache 实例使用以下文件夹:

2022-09-05 00:28:40

我有这个错误“ 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);

答案 1

您需要创建缓存类,以确保在所有应用程序中都有一个实例:SingletonSimpleCache

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"));

答案 2

使对象单例不一定能解决问题,因为由于之前的音频播放器调用,仍可以锁定 对象。发生这种情况时,仍将抛出 。cachecacheIllegalStateException

要解决这个问题,您需要在释放播放器时释放,即将其放在播放器的重写方法中:cacherelease

        cache.release();
        cache = null;

推荐