java.lang.IllegalStateException : Another SimpleCache instance uses the folder: 错误

标签 java android

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

最佳答案

您需要将您的缓存类设置为 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"));

关于java.lang.IllegalStateException : Another SimpleCache instance uses the folder: 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52507270/

相关文章:

java - 我想验证网站的菜单栏选项卡而不是单击它们

java - 什么是最好的开源 Java 图表库? (除了 jfreechart)

android ndk 链接到 libcutils.so

java - 如何防止在 Backendless 发送和创建对象的属性

java - 致命异常 : AsyncTask #1 while implementing RSS Reader

android - 需要一个字符串,但在 $.Result 处是 BEGIN_OBJECT

java - 升级到 macOS High Sierra 后 Web 应用程序未正确部署

java - 在 Shell 脚本中使用带空格的路径

android - 如何让我的 Android 应用程序在 RTL 和 LTR 语言上看起来一样?

java - 如何在heroku中找到webservice路径?