Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

I have this Error " java.lang.IllegalStateException: Another SimpleCache instance uses the folder:" i am working with SimpleExoPlayer and this error show when i try to open the video for second time how to close or delete the previous simplecache ? and this is my code :

 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);
like image 565
ahmad Avatar asked Sep 25 '18 22:09

ahmad


2 Answers

You need to make your cache class Singleton to make sure you have one instance of SimpleCache across all your application:

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;
    }
}

And use it in your code like:

DataSource.Factory dataSourceFactory = new CacheDataSourceFactory(VideoCache.getInstance(this), new DefaultDataSourceFactory(this, "seyed"));
like image 159
Sdghasemi Avatar answered Nov 06 '22 21:11

Sdghasemi


Making the cache object singleton does not necessarily solve the problem because the cache can still be locked due to a previous audio player call. When this happens, the IllegalStateException will still be thrown.

To solve this, you need to release the cache when releasing the player, i.e. put this in the overridden release method of the player:

        cache.release();
        cache = null;
like image 43
Ali Nem Avatar answered Nov 06 '22 21:11

Ali Nem