merge media sourcess

This commit is contained in:
Bnyro 2022-09-09 18:00:52 +02:00
parent 79cc9055b2
commit 9e414a23cf

View File

@ -15,7 +15,10 @@ import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding
import com.github.libretube.extensions.BaseActivity import com.github.libretube.extensions.BaseActivity
import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.source.MergingMediaSource
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.ui.StyledPlayerView import com.google.android.exoplayer2.ui.StyledPlayerView
import com.google.android.exoplayer2.upstream.FileDataSource
import java.io.File import java.io.File
class OfflinePlayerActivity : BaseActivity() { class OfflinePlayerActivity : BaseActivity() {
@ -65,39 +68,64 @@ class OfflinePlayerActivity : BaseActivity() {
} }
private fun playVideo() { private fun playVideo() {
val downloadDir = File( val videoDownloadDir = File(
getExternalFilesDir(null), getExternalFilesDir(null),
"video" "video"
) )
val file = File( val videoFile = File(
downloadDir, videoDownloadDir,
fileName fileName
) )
val audioDownloadDir = File(
getExternalFilesDir(null),
"audio"
)
val audioFile = File(
audioDownloadDir,
fileName
)
val videoUri = if (videoFile.exists()) Uri.fromFile(videoFile) else null
val audioUri = if (audioFile.exists()) Uri.fromFile(audioFile) else null
setMediaSource( setMediaSource(
Uri.fromFile(file) videoUri,
audioUri
) )
player.prepare() player.prepare()
player.play() player.play()
} }
private fun setMediaSource(uri: Uri) { private fun setMediaSource(videoUri: Uri?, audioUri: Uri?) {
val mediaItem = MediaItem when {
.fromUri(uri) videoUri != null && audioUri != null -> {
val videoSource = ProgressiveMediaSource.Factory(FileDataSource.Factory())
.createMediaSource(
MediaItem.fromUri(videoUri)
)
player.setMediaItem(mediaItem) val audioSource = ProgressiveMediaSource.Factory(FileDataSource.Factory())
.createMediaSource(
MediaItem.fromUri(audioUri)
)
/* val mediaSource = MergingMediaSource(
val audioSource: MediaSource = audioSource,
ProgressiveMediaSource.Factory(dataSourceFactory) videoSource
.createMediaSource(MediaItem.fromUri(audioUrl)) )
val mergeSource: MediaSource =
MergingMediaSource(videoSource, audioSource)
player.setMediaSource(mergeSource)
*/ player.setMediaSource(mediaSource)
}
videoUri != null -> player.setMediaItem(
MediaItem.fromUri(videoUri)
)
audioUri != null -> player.setMediaItem(
MediaItem.fromUri(audioUri)
)
}
} }
private fun hideSystemBars() { private fun hideSystemBars() {