-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to listen to cached songs, very experimental
- Loading branch information
1 parent
823ae8f
commit be26dd3
Showing
12 changed files
with
125 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
app/src/main/java/dev/brahmkshatriya/echo/playback/source/CustomCacheDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package dev.brahmkshatriya.echo.playback.source | ||
|
||
import android.net.Uri | ||
import androidx.annotation.OptIn | ||
import androidx.media3.common.util.UnstableApi | ||
import androidx.media3.datasource.BaseDataSource | ||
import androidx.media3.datasource.DataSource | ||
import androidx.media3.datasource.DataSpec | ||
import androidx.media3.datasource.cache.CacheDataSource | ||
import androidx.media3.datasource.cache.SimpleCache | ||
import dev.brahmkshatriya.echo.playback.source.MediaResolver.Companion.LOCAL | ||
|
||
@OptIn(UnstableApi::class) | ||
class CustomCacheDataSource( | ||
cache: SimpleCache, | ||
private val upstream: DataSource.Factory | ||
) : BaseDataSource(true) { | ||
|
||
class Factory( | ||
private val cache: SimpleCache, | ||
private val upstream: DataSource.Factory | ||
) : DataSource.Factory { | ||
override fun createDataSource() = CustomCacheDataSource(cache, upstream) | ||
} | ||
|
||
private val cacheFactory = CacheDataSource | ||
.Factory().setCache(cache) | ||
.setUpstreamDataSourceFactory(upstream) | ||
|
||
var source: DataSource? = null | ||
override fun read(buffer: ByteArray, offset: Int, length: Int): Int { | ||
return source?.read(buffer, offset, length) ?: throw Exception("Source not opened") | ||
} | ||
|
||
override fun open(dataSpec: DataSpec): Long { | ||
val source = if (dataSpec.uri == LOCAL) upstream.createDataSource() | ||
else cacheFactory.createDataSource() | ||
this.source = source | ||
return source.open(dataSpec) | ||
} | ||
|
||
override fun getUri(): Uri? { | ||
return source?.uri | ||
} | ||
|
||
override fun close() { | ||
source?.close() | ||
source = null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters