mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 14:20:30 +05:30
Merge pull request #2656 from Bnyro/master
Get available resolutions dynamically from player
This commit is contained in:
commit
6e62a328c1
@ -1202,63 +1202,34 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
|
|||||||
exoPlayer.setMediaItem(mediaItem)
|
exoPlayer.setMediaItem(mediaItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun String?.qualityToInt(): Int {
|
/**
|
||||||
this ?: return 0
|
* Get all available player resolutions
|
||||||
return this.toString().split("p").first().toInt()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getAvailableResolutions(): List<VideoResolution> {
|
|
||||||
if (!this::streams.isInitialized) return listOf()
|
|
||||||
|
|
||||||
val resolutions = mutableListOf<VideoResolution>()
|
|
||||||
|
|
||||||
val videoStreams = try {
|
|
||||||
// attempt to sort the qualities, catch if there was an error ih parsing
|
|
||||||
streams.videoStreams?.sortedBy {
|
|
||||||
it.quality?.toLong() ?: 0L
|
|
||||||
}?.reversed()
|
|
||||||
.orEmpty()
|
|
||||||
} catch (_: Exception) {
|
|
||||||
streams.videoStreams.orEmpty()
|
|
||||||
}
|
|
||||||
|
|
||||||
for (vid in videoStreams) {
|
|
||||||
if (resolutions.any {
|
|
||||||
it.resolution == vid.quality.qualityToInt()
|
|
||||||
} || vid.url == null
|
|
||||||
) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
runCatching {
|
|
||||||
resolutions.add(
|
|
||||||
VideoResolution(
|
|
||||||
name = "${vid.quality.qualityToInt()}p",
|
|
||||||
resolution = vid.quality.qualityToInt()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
// append quality to list if it has the preferred format (e.g. MPEG)
|
|
||||||
val preferredMimeType = "video/${PlayerHelper.videoFormatPreference}"
|
|
||||||
if (vid.url != null && vid.mimeType == preferredMimeType)
|
|
||||||
*/
|
*/
|
||||||
|
private fun getAvailableResolutions(): List<VideoResolution> {
|
||||||
|
val resolutions = exoPlayer.currentTracks.groups.map { group ->
|
||||||
|
(0..group.length - 1).map {
|
||||||
|
group.getTrackFormat(it).width
|
||||||
}
|
}
|
||||||
|
}.flatten()
|
||||||
|
.filter { it > 0 }
|
||||||
|
.sortedDescending()
|
||||||
|
.toSet()
|
||||||
|
.toList()
|
||||||
|
|
||||||
if (resolutions.isEmpty()) {
|
return resolutions.map {
|
||||||
return listOf(
|
|
||||||
VideoResolution(
|
VideoResolution(
|
||||||
getString(R.string.hls),
|
name = "${it}p",
|
||||||
resolution = Int.MAX_VALUE,
|
resolution = it
|
||||||
adaptiveSourceUrl = streams.hls
|
)
|
||||||
|
}.toMutableList().also {
|
||||||
|
it.add(
|
||||||
|
0,
|
||||||
|
VideoResolution(
|
||||||
|
getString(R.string.auto_quality),
|
||||||
|
resolution = Int.MAX_VALUE
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
resolutions.add(0, VideoResolution(getString(R.string.auto_quality), Int.MAX_VALUE))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return resolutions
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setResolutionAndSubtitles() {
|
private fun setResolutionAndSubtitles() {
|
||||||
|
Loading…
Reference in New Issue
Block a user