rework: getting original resolution for quality selection dialog

This commit is contained in:
nomadics9 2024-07-20 23:55:34 +03:00
parent c79342523b
commit 21ae815223
3 changed files with 24 additions and 10 deletions

View file

@ -353,14 +353,14 @@ class PlayerActivity : BasePlayerActivity() {
private var selectedIndex = 1 // Default to "Original" (index 1)
private fun showQualitySelectionDialog() {
val originalHeight = viewModel.getOriginalHeight() // TODO: Rework getting originalHeight
val originalResolution = viewModel.getoriginalResolution() // TODO: Rework getting originalResolution
val qualityEntries = resources.getStringArray(CoreR.array.quality_entries).toList()
val qualityValues = resources.getStringArray(CoreR.array.quality_values).toList()
val qualities = qualityEntries.toMutableList()
val closestQuality = VideoQuality.entries
.filter { it != VideoQuality.Auto && it != VideoQuality.Original }
.minByOrNull { kotlin.math.abs(it.height - originalHeight) }
.minByOrNull { kotlin.math.abs(it.height*it.width - originalResolution!!) }
if (closestQuality != null) {
qualities[1] = "${qualities[1]} (${closestQuality})"

View file

@ -8,6 +8,7 @@ enum class VideoQuality(
) {
Auto(10000000, 1080, 1920, false),
Original(1000000000, 1080, 1920, true),
P3840(12000000,3840, 2160, false), // Here for future proofing and to calculate original resolution only
P1080(8000000, 1080, 1920, false),
P720(3000000, 720, 1280, false),
P480(1500000, 480, 854, false),
@ -16,6 +17,7 @@ enum class VideoQuality(
override fun toString(): String = when (this) {
Auto -> "Auto"
Original -> "Original"
P3840 -> "4K"
else -> "${height}p"
}

View file

@ -18,6 +18,7 @@ import androidx.media3.common.MimeTypes
import androidx.media3.common.Player
import androidx.media3.common.TrackSelectionOverride
import androidx.media3.common.TrackSelectionParameters
import androidx.media3.common.VideoSize
import androidx.media3.exoplayer.DefaultRenderersFactory
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector
@ -61,7 +62,7 @@ constructor(
private val savedStateHandle: SavedStateHandle,
) : ViewModel(), Player.Listener {
val player: Player
private var originalHeight: Int = 0
private var originalResolution: Int? = null
private val _uiState = MutableStateFlow(
UiState(
@ -179,6 +180,22 @@ constructor(
.setSubtitleConfigurations(mediaSubtitles)
.build()
mediaItems.add(mediaItem)
player.addListener(object : Player.Listener {
override fun onPlaybackStateChanged(state: Int) {
if (state == Player.STATE_READY) {
val videoSize = player.videoSize
val initialHeight = videoSize.height
val initialWidth = videoSize.width
originalResolution = initialHeight * initialWidth
Timber.d("Initial video size: $initialWidth x $initialHeight")
player.removeListener(this)
}
}
})
}
} catch (e: Exception) {
Timber.e(e)
@ -564,13 +581,8 @@ constructor(
playWhenReady = true
player.play()
val originalHeight = mediaSources[currentMediaItemIndex].mediaStreams
.filter { it.type == MediaStreamType.VIDEO }
.map {mediaStream -> mediaStream.height}.first() ?: 1080
// Store the original height
this@PlayerActivityViewModel.originalHeight = originalHeight
//isQualityChangeInProgress = true
} catch (e: Exception) {
@ -579,8 +591,8 @@ constructor(
}
}
fun getOriginalHeight(): Int {
return originalHeight
fun getoriginalResolution(): Int? {
return originalResolution
}
}