fix defres

This commit is contained in:
Bnyro 2022-07-14 19:48:00 +02:00
parent 34a881fbaa
commit 9000745759
2 changed files with 54 additions and 51 deletions

View File

@ -140,6 +140,7 @@ class PlayerFragment : Fragment() {
private lateinit var thumbnailUrl: String private lateinit var thumbnailUrl: String
private lateinit var chapters: List<ChapterSegment> private lateinit var chapters: List<ChapterSegment>
private val sponsorBlockPrefs = SponsorBlockPrefs() private val sponsorBlockPrefs = SponsorBlockPrefs()
private lateinit var subtitle: MutableList<SubtitleConfiguration>
private var autoRotationEnabled = true private var autoRotationEnabled = true
@ -1093,7 +1094,6 @@ class PlayerFragment : Fragment() {
} }
private fun setMediaSource( private fun setMediaSource(
subtitle: MutableList<SubtitleConfiguration>,
videoUri: Uri, videoUri: Uri,
audioUrl: String audioUrl: String
) { ) {
@ -1117,7 +1117,6 @@ class PlayerFragment : Fragment() {
private fun setResolutionAndSubtitles(response: Streams) { private fun setResolutionAndSubtitles(response: Streams) {
val videoFormatPreference = val videoFormatPreference =
PreferenceHelper.getString(requireContext(), "player_video_format", "WEBM") PreferenceHelper.getString(requireContext(), "player_video_format", "WEBM")
val defres = PreferenceHelper.getString(requireContext(), "default_res", "")!!
var videosNameArray: Array<CharSequence> = arrayOf() var videosNameArray: Array<CharSequence> = arrayOf()
var videosUrlArray: Array<Uri> = arrayOf() var videosUrlArray: Array<Uri> = arrayOf()
@ -1133,13 +1132,13 @@ class PlayerFragment : Fragment() {
if (vid.format.equals(videoFormatPreference) && vid.url != null) { // preferred format if (vid.format.equals(videoFormatPreference) && vid.url != null) { // preferred format
videosNameArray += vid.quality.toString() videosNameArray += vid.quality.toString()
videosUrlArray += vid.url!!.toUri() videosUrlArray += vid.url!!.toUri()
} else if (vid.quality.equals("LBRY") && vid.format.equals("MP4")) { // LBRY MP4 format) } else if (vid.quality.equals("LBRY") && vid.format.equals("MP4")) { // LBRY MP4 format
videosNameArray += "LBRY MP4" videosNameArray += "LBRY MP4"
videosUrlArray += vid.url!!.toUri() videosUrlArray += vid.url!!.toUri()
} }
} }
// create a list of subtitles // create a list of subtitles
val subtitle = mutableListOf<SubtitleConfiguration>() subtitle = mutableListOf<SubtitleConfiguration>()
response.subtitles!!.forEach { response.subtitles!!.forEach {
subtitle.add( subtitle.add(
SubtitleConfiguration.Builder(it.url!!.toUri()) SubtitleConfiguration.Builder(it.url!!.toUri())
@ -1148,50 +1147,12 @@ class PlayerFragment : Fragment() {
.build() .build()
) )
} }
// set resolution in the beginning // set media source and resolution in the beginning
when { setStreamSource(
// search for the default resolution in the videoNamesArray, select quality if found response,
defres != "" -> { videosNameArray,
run lit@{ videosUrlArray
videosNameArray.forEachIndexed { index, pipedStream -> )
if (pipedStream.contains(defres)) {
val videoUri = videosUrlArray[index]
val audioUrl = getMostBitRate(response.audioStreams!!)
setMediaSource(subtitle, videoUri, audioUrl)
playerBinding.qualityText.text = videosNameArray[index]
return@lit
} else if (response.hls != null) {
val mediaItem: MediaItem = MediaItem.Builder()
.setUri(response.hls)
.setSubtitleConfigurations(subtitle)
.build()
exoPlayer.setMediaItem(mediaItem)
} else {
Toast.makeText(
context,
getString(R.string.unknown_error),
Toast.LENGTH_LONG
).show()
}
}
}
}
// if defres doesn't match use hls if available
response.hls != null -> {
val mediaItem: MediaItem = MediaItem.Builder()
.setUri(response.hls)
.setSubtitleConfigurations(subtitle)
.build()
exoPlayer.setMediaItem(mediaItem)
}
// otherwise use the first list entry
else -> {
val videoUri = videosUrlArray[0]
val audioUrl = getMostBitRate(response.audioStreams!!)
setMediaSource(subtitle, videoUri, audioUrl)
playerBinding.qualityText.text = videosNameArray[0]
}
}
playerBinding.qualityText.setOnClickListener { playerBinding.qualityText.setOnClickListener {
// Dialog for quality selection // Dialog for quality selection
@ -1217,7 +1178,7 @@ class PlayerFragment : Fragment() {
} else { } else {
val videoUri = videosUrlArray[which] val videoUri = videosUrlArray[which]
val audioUrl = getMostBitRate(response.audioStreams!!) val audioUrl = getMostBitRate(response.audioStreams!!)
setMediaSource(subtitle, videoUri, audioUrl) setMediaSource(videoUri, audioUrl)
} }
exoPlayer.seekTo(lastPosition) exoPlayer.seekTo(lastPosition)
playerBinding.qualityText.text = videosNameArray[which] playerBinding.qualityText.text = videosNameArray[which]
@ -1227,6 +1188,48 @@ class PlayerFragment : Fragment() {
} }
} }
private fun setStreamSource(
streams: Streams,
videosNameArray: Array<CharSequence>,
videosUrlArray: Array<Uri>
) {
val defRes = PreferenceHelper.getString(
requireContext(),
"default_resolution",
"auto"
)!!
if (defRes != "auto") {
videosNameArray.forEachIndexed { index, pipedStream ->
// search for quality preference in the available stream sources
if (pipedStream.contains(defRes)) {
val videoUri = videosUrlArray[index]
val audioUrl = getMostBitRate(streams.audioStreams!!)
setMediaSource(videoUri, audioUrl)
playerBinding.qualityText.text = videosNameArray[index]
return
}
}
}
// if default resolution isn't set or available, use hls if available
if (streams.hls != null) {
val mediaItem: MediaItem = MediaItem.Builder()
.setUri(streams.hls)
.setSubtitleConfigurations(subtitle)
.build()
exoPlayer.setMediaItem(mediaItem)
playerBinding.qualityText.text = context?.getString(R.string.hls)
return
}
// if nothing found, use the first list entry
if (videosUrlArray.isNotEmpty()) {
val videoUri = videosUrlArray[0]
val audioUrl = getMostBitRate(streams.audioStreams!!)
setMediaSource(videoUri, audioUrl)
playerBinding.qualityText.text = videosNameArray[0]
}
}
private fun createExoPlayer(view: View) { private fun createExoPlayer(view: View) {
val bufferingGoal = val bufferingGoal =
PreferenceHelper.getString(requireContext(), "buffering_goal", "50")?.toInt()!! * 1000 PreferenceHelper.getString(requireContext(), "buffering_goal", "50")?.toInt()!! * 1000

View File

@ -6,10 +6,10 @@
<ListPreference <ListPreference
android:icon="@drawable/ic_hd" android:icon="@drawable/ic_hd"
app:defaultValue="" app:defaultValue="hls"
app:entries="@array/defres" app:entries="@array/defres"
app:entryValues="@array/defresValue" app:entryValues="@array/defresValue"
app:key="default_res" app:key="default_resolution"
app:title="@string/defres" app:title="@string/defres"
app:useSimpleSummaryProvider="true" /> app:useSimpleSummaryProvider="true" />