change MediaRepository so it would fit what NewPipe expects

This commit is contained in:
Christian Schabesberger 2024-08-05 15:25:44 +02:00
parent bdbd8caf43
commit ce6ef8a8fd
5 changed files with 41 additions and 17 deletions

View File

@ -2,6 +2,7 @@ package net.newpipe.newplayer
import android.graphics.Bitmap
import android.net.Uri
import androidx.media3.common.MediaItem
interface MediaRepository {
suspend fun getTitle(item: String) : String
@ -10,7 +11,7 @@ interface MediaRepository {
suspend fun getAvailableStreams(item: String): List<String>
suspend fun getStream(item: String, streamSelector: String) : String
suspend fun getStream(item: String, streamSelector: String) : MediaItem
suspend fun getLinkWithStreamOffset(item: String) : String
suspend fun getPreviewThumbnails(item: String) : List<Bitmap>

View File

@ -21,9 +21,11 @@
package net.newpipe.newplayer
import android.app.Application
import android.util.Log
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.source.MediaSource
import java.lang.Exception
enum class PlayMode {
@ -34,6 +36,8 @@ enum class PlayMode {
AUDIO_FORGROUND,
}
private val TAG = "NewPlayer"
interface NewPlayer {
val internal_player: Player
var playWhenReady: Boolean
@ -54,11 +58,21 @@ interface NewPlayer {
fun addListener(callbackListener: Listener)
//TODO: This is only temporary
fun setStream(uri: String)
fun setStream(stream: MediaItem)
data class Builder(val app: Application, val repository: MediaRepository) {
private var mediaSourceFactory : MediaSource.Factory? = null
fun setMediaSourceFactory(mediaSourceFactory: MediaSource.Factory) {
this.mediaSourceFactory = mediaSourceFactory
}
fun build(): NewPlayer {
return NewPlayerImpl(ExoPlayer.Builder(app).build(), repository = repository)
val exoPlayerBuilder = ExoPlayer.Builder(app)
mediaSourceFactory?.let {
exoPlayerBuilder.setMediaSourceFactory(it)
}
return NewPlayerImpl(exoPlayerBuilder.build(), repository = repository)
}
}
@ -86,7 +100,11 @@ class NewPlayerImpl(override val internal_player: Player, override val repositor
}
override fun play() {
internal_player.play()
if(internal_player.currentMediaItem != null) {
internal_player.play()
} else {
Log.i(TAG, "Tried to start playing but no media Item was cued")
}
}
override fun pause() {
@ -110,11 +128,11 @@ class NewPlayerImpl(override val internal_player: Player, override val repositor
}
override fun setStream(uri: String) {
override fun setStream(stream: MediaItem) {
if (internal_player.playbackState == Player.STATE_IDLE) {
internal_player.prepare()
}
internal_player.setMediaItem(MediaItem.fromUri(uri))
internal_player.setMediaItem(stream)
}
}

View File

@ -30,6 +30,7 @@ import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.media3.common.MediaItem
import dagger.hilt.android.AndroidEntryPoint
import net.newpipe.newplayer.ActivityBrainSlug
import net.newpipe.newplayer.NewPlayer
@ -62,7 +63,7 @@ class MainActivity : AppCompatActivity() {
startStreamButton.setOnClickListener {
newPlayer.playWhenReady = true
newPlayer.setStream(getString(R.string.ccc_6502_video))
newPlayer.setStream(MediaItem.fromUri(getString(R.string.ccc_6502_video)))
}
videoPlayerViewModel.newPlayer = newPlayer

View File

@ -34,6 +34,6 @@ object NewPlayerComponent {
@Provides
@Singleton
fun provideNewPlayer(app: Application) : NewPlayer {
return NewPlayer.Builder(app).build()
return NewPlayer.Builder(app, TestMediaRepository(app)).build()
}
}

View File

@ -5,6 +5,7 @@ import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.media.Image
import android.net.Uri
import androidx.media3.common.MediaItem
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import net.newpipe.newplayer.MediaRepository
@ -76,16 +77,19 @@ class TestMediaRepository(val context: Context) : MediaRepository {
override suspend fun getStream(item: String, streamSelector: String) =
when (item) {
"6502" -> context.getString(R.string.ccc_6502_video)
"portrait" -> context.getString(R.string.portrait_video_example)
"imu" -> when(streamSelector) {
"1080p" -> context.getString(R.string.ccc_imu_1080_mp4)
"576p" -> context.getString(R.string.ccc_imu_576_mp4)
else -> throw Exception("Unknown stream selector for $item: $streamSelector")
MediaItem.fromUri(
when (item) {
"6502" -> context.getString(R.string.ccc_6502_video)
"portrait" -> context.getString(R.string.portrait_video_example)
"imu" -> when (streamSelector) {
"1080p" -> context.getString(R.string.ccc_imu_1080_mp4)
"576p" -> context.getString(R.string.ccc_imu_576_mp4)
else -> throw Exception("Unknown stream selector for $item: $streamSelector")
}
else -> throw Exception("Unknown stream: $item")
}
else -> throw Exception("Unknown stream: $item")
}
)
override suspend fun getLinkWithStreamOffset(item: String): String {
TODO("Not yet implemented")