Skip to content

Commit

Permalink
Use createDataSourceFactory internally
Browse files Browse the repository at this point in the history
  • Loading branch information
qnga committed Jul 1, 2024
1 parent d1782a9 commit dde13c8
Showing 1 changed file with 3 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
package org.readium.adapter.exoplayer.audio

import android.app.Application
import androidx.media3.datasource.DataSource
import androidx.media3.datasource.FileDataSource
import androidx.media3.datasource.cache.CacheDataSink
import androidx.media3.datasource.cache.CacheDataSource
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import org.readium.navigator.media.audio.AudioEngineProvider
Expand All @@ -25,7 +21,6 @@ import org.readium.r2.shared.publication.Locator
import org.readium.r2.shared.publication.Publication
import org.readium.r2.shared.publication.indexOfFirstWithHref
import org.readium.r2.shared.util.Try
import org.readium.r2.shared.util.toUrl

/**
* Main component to use the audio navigator with the ExoPlayer adapter.
Expand All @@ -52,7 +47,9 @@ public class ExoPlayerEngineProvider(
): Try<ExoPlayerEngine, Nothing> {
val metadataFactory = metadataProvider.createMetadataFactory(publication)
val settingsResolver = ExoPlayerSettingsResolver(defaults)
val dataSourceFactory = createDataSourceFactory(publication)
val dataSourceFactory = cacheProvider
?.createCacheDataSourceFactory(publication)
?: ExoPlayerDataSource.Factory(publication)
val initialIndex = publication.readingOrder.indexOfFirstWithHref(initialLocator.href) ?: 0
val initialPosition = initialLocator.locations.time ?: Duration.ZERO
val playlist = ExoPlayerEngine.Playlist(
Expand Down Expand Up @@ -81,22 +78,6 @@ public class ExoPlayerEngineProvider(
return Try.success(engine)
}

private fun createDataSourceFactory(
publication: Publication
): DataSource.Factory {
val baseDataSource = ExoPlayerDataSource.Factory(publication)
val cache = cacheProvider?.getCache(publication)
?: return baseDataSource

return CacheDataSource.Factory()
.setCache(cache)
.setCacheKeyFactory { cacheProvider.computeKey(publication, it.uri.toUrl()!!) }
.setCacheWriteDataSinkFactory(CacheDataSink.Factory().setCache(cache))
.setCacheReadDataSourceFactory(FileDataSource.Factory())
.setUpstreamDataSourceFactory(baseDataSource)
.setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
}

override fun createPreferenceEditor(
publication: Publication,
initialPreferences: ExoPlayerPreferences
Expand Down

0 comments on commit dde13c8

Please sign in to comment.