mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 00:10:32 +05:30
added player video format option
This commit is contained in:
parent
3f900d5be7
commit
456ef1e6c0
@ -642,13 +642,53 @@ class PlayerFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setResolutionAndSubtitles(view: View, response: Streams) {
|
private fun setMediaSource(
|
||||||
var videosNameArray: Array<CharSequence> = arrayOf()
|
streams: Streams,
|
||||||
videosNameArray += "HLS"
|
subtitle: MutableList<SubtitleConfiguration>,
|
||||||
for (vid in response.videoStreams!!) {
|
videoUri: Uri
|
||||||
val name = vid.quality + " " + vid.format
|
) {
|
||||||
videosNameArray += name
|
val dataSourceFactory: DataSource.Factory =
|
||||||
|
DefaultHttpDataSource.Factory()
|
||||||
|
val videoItem: MediaItem = MediaItem.Builder()
|
||||||
|
.setUri(videoUri)
|
||||||
|
.setSubtitleConfigurations(subtitle)
|
||||||
|
.build()
|
||||||
|
val videoSource: MediaSource =
|
||||||
|
DefaultMediaSourceFactory(dataSourceFactory)
|
||||||
|
.createMediaSource(videoItem)
|
||||||
|
var audioSource: MediaSource =
|
||||||
|
ProgressiveMediaSource.Factory(dataSourceFactory)
|
||||||
|
.createMediaSource(
|
||||||
|
fromUri(
|
||||||
|
streams.audioStreams!![
|
||||||
|
getMostBitRate(
|
||||||
|
streams.audioStreams
|
||||||
|
)
|
||||||
|
].url!!
|
||||||
|
)
|
||||||
|
)
|
||||||
|
val mergeSource: MediaSource =
|
||||||
|
MergingMediaSource(videoSource, audioSource)
|
||||||
|
exoPlayer.setMediaSource(mergeSource)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun setResolutionAndSubtitles(view: View, response: Streams) {
|
||||||
|
val sharedPreferences =
|
||||||
|
PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||||
|
|
||||||
|
var videosNameArray: Array<CharSequence> = arrayOf()
|
||||||
|
var videosUrlArray: Array<Uri> = arrayOf()
|
||||||
|
videosNameArray += "HLS"
|
||||||
|
|
||||||
|
// append quality to list if it has the preferred format
|
||||||
|
val videoFormatPreference = sharedPreferences.getString("player_video_format", "WEBM")
|
||||||
|
for (vid in response.videoStreams!!) {
|
||||||
|
if (vid.format.equals(videoFormatPreference)) {
|
||||||
|
videosNameArray += vid.quality!!
|
||||||
|
videosUrlArray += vid.url!!.toUri()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// create a list of subtitles
|
||||||
val subtitle = mutableListOf<SubtitleConfiguration>()
|
val subtitle = mutableListOf<SubtitleConfiguration>()
|
||||||
if (response.subtitles!!.isNotEmpty()) {
|
if (response.subtitles!!.isNotEmpty()) {
|
||||||
subtitle.add(
|
subtitle.add(
|
||||||
@ -658,49 +698,18 @@ class PlayerFragment : Fragment() {
|
|||||||
.build()
|
.build()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
val sharedPreferences =
|
// set resolution in the beginning
|
||||||
PreferenceManager.getDefaultSharedPreferences(requireContext())
|
|
||||||
val defres = sharedPreferences.getString("default_res", "")!!
|
val defres = sharedPreferences.getString("default_res", "")!!
|
||||||
when {
|
when {
|
||||||
|
// search for the default resolution in the videoNamesArray, select quality if found
|
||||||
defres != "" -> {
|
defres != "" -> {
|
||||||
run lit@{
|
run lit@{
|
||||||
response.videoStreams.forEachIndexed { index, pipedStream ->
|
videosNameArray.forEachIndexed { index, pipedStream ->
|
||||||
if (pipedStream.quality!!.contains(defres)) {
|
if (pipedStream.contains(defres)) {
|
||||||
val dataSourceFactory: DataSource.Factory =
|
val videoUri = videosUrlArray[index - 1]
|
||||||
DefaultHttpDataSource.Factory()
|
setMediaSource(response, subtitle, videoUri)
|
||||||
val videoItem: MediaItem = MediaItem.Builder()
|
|
||||||
.setUri(response.videoStreams[index].url)
|
|
||||||
.setSubtitleConfigurations(subtitle)
|
|
||||||
.build()
|
|
||||||
val videoSource: MediaSource =
|
|
||||||
DefaultMediaSourceFactory(dataSourceFactory)
|
|
||||||
.createMediaSource(videoItem)
|
|
||||||
var audioSource: MediaSource =
|
|
||||||
DefaultMediaSourceFactory(dataSourceFactory)
|
|
||||||
.createMediaSource(
|
|
||||||
fromUri(response.audioStreams!![0].url!!)
|
|
||||||
)
|
|
||||||
if (response.videoStreams[index].quality == "720p" ||
|
|
||||||
response.videoStreams[index].quality == "1080p" ||
|
|
||||||
response.videoStreams[index].quality == "480p"
|
|
||||||
) {
|
|
||||||
audioSource =
|
|
||||||
ProgressiveMediaSource.Factory(dataSourceFactory)
|
|
||||||
.createMediaSource(
|
|
||||||
fromUri(
|
|
||||||
response.audioStreams[
|
|
||||||
getMostBitRate(
|
|
||||||
response.audioStreams
|
|
||||||
)
|
|
||||||
].url!!
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
val mergeSource: MediaSource =
|
|
||||||
MergingMediaSource(videoSource, audioSource)
|
|
||||||
exoPlayer.setMediaSource(mergeSource)
|
|
||||||
view.findViewById<TextView>(R.id.quality_text).text =
|
view.findViewById<TextView>(R.id.quality_text).text =
|
||||||
videosNameArray[index + 1]
|
videosNameArray[index]
|
||||||
return@lit
|
return@lit
|
||||||
} else if (index + 1 == response.videoStreams.size) {
|
} else if (index + 1 == response.videoStreams.size) {
|
||||||
val mediaItem: MediaItem = MediaItem.Builder()
|
val mediaItem: MediaItem = MediaItem.Builder()
|
||||||
@ -720,36 +729,8 @@ class PlayerFragment : Fragment() {
|
|||||||
exoPlayer.setMediaItem(mediaItem)
|
exoPlayer.setMediaItem(mediaItem)
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
val dataSourceFactory: DataSource.Factory =
|
val videoUri = response.videoStreams[1].url?.toUri()!!
|
||||||
DefaultHttpDataSource.Factory()
|
setMediaSource(response, subtitle, videoUri)
|
||||||
val videoItem: MediaItem = MediaItem.Builder()
|
|
||||||
.setUri(response.videoStreams[0].url)
|
|
||||||
.setSubtitleConfigurations(subtitle)
|
|
||||||
.build()
|
|
||||||
val videoSource: MediaSource =
|
|
||||||
DefaultMediaSourceFactory(dataSourceFactory)
|
|
||||||
.createMediaSource(videoItem)
|
|
||||||
var audioSource: MediaSource =
|
|
||||||
DefaultMediaSourceFactory(dataSourceFactory)
|
|
||||||
.createMediaSource(fromUri(response.audioStreams!![0].url!!))
|
|
||||||
if (response.videoStreams[0].quality == "720p" ||
|
|
||||||
response.videoStreams[0].quality == "1080p" ||
|
|
||||||
response.videoStreams[0].quality == "480p"
|
|
||||||
) {
|
|
||||||
audioSource = ProgressiveMediaSource.Factory(dataSourceFactory)
|
|
||||||
.createMediaSource(
|
|
||||||
fromUri(
|
|
||||||
response.audioStreams[
|
|
||||||
getMostBitRate(
|
|
||||||
response.audioStreams
|
|
||||||
)
|
|
||||||
].url!!
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
val mergeSource: MediaSource =
|
|
||||||
MergingMediaSource(videoSource, audioSource)
|
|
||||||
exoPlayer.setMediaSource(mergeSource)
|
|
||||||
view.findViewById<TextView>(R.id.quality_text).text = videosNameArray[1]
|
view.findViewById<TextView>(R.id.quality_text).text = videosNameArray[1]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -765,18 +746,6 @@ class PlayerFragment : Fragment() {
|
|||||||
videosNameArray
|
videosNameArray
|
||||||
) { _, which ->
|
) { _, which ->
|
||||||
whichQuality = which
|
whichQuality = which
|
||||||
if (response.subtitles.isNotEmpty()) {
|
|
||||||
val subtitle =
|
|
||||||
mutableListOf<SubtitleConfiguration>()
|
|
||||||
subtitle.add(
|
|
||||||
SubtitleConfiguration.Builder(
|
|
||||||
response.subtitles[0].url!!.toUri()
|
|
||||||
)
|
|
||||||
.setMimeType(response.subtitles[0].mimeType!!) // The correct MIME type (required).
|
|
||||||
.setLanguage(response.subtitles[0].code) // The subtitle language (optional).
|
|
||||||
.build()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (which == 0) {
|
if (which == 0) {
|
||||||
val mediaItem: MediaItem = MediaItem.Builder()
|
val mediaItem: MediaItem = MediaItem.Builder()
|
||||||
.setUri(response.hls)
|
.setUri(response.hls)
|
||||||
@ -784,43 +753,11 @@ class PlayerFragment : Fragment() {
|
|||||||
.build()
|
.build()
|
||||||
exoPlayer.setMediaItem(mediaItem)
|
exoPlayer.setMediaItem(mediaItem)
|
||||||
} else {
|
} else {
|
||||||
val dataSourceFactory: DataSource.Factory =
|
val videoUri = videosUrlArray[which - 1]
|
||||||
DefaultHttpDataSource.Factory()
|
setMediaSource(response, subtitle, videoUri)
|
||||||
val videoItem: MediaItem = MediaItem.Builder()
|
|
||||||
.setUri(response.videoStreams[which - 1].url)
|
|
||||||
.setSubtitleConfigurations(subtitle)
|
|
||||||
.build()
|
|
||||||
val videoSource: MediaSource =
|
|
||||||
DefaultMediaSourceFactory(dataSourceFactory)
|
|
||||||
.createMediaSource(videoItem)
|
|
||||||
var audioSource: MediaSource =
|
|
||||||
DefaultMediaSourceFactory(dataSourceFactory)
|
|
||||||
.createMediaSource(
|
|
||||||
fromUri(response.audioStreams!![0].url!!)
|
|
||||||
)
|
|
||||||
if (response.videoStreams[which - 1].quality == "720p" ||
|
|
||||||
response.videoStreams[which - 1].quality == "1080p" ||
|
|
||||||
response.videoStreams[which - 1].quality == "480p"
|
|
||||||
) {
|
|
||||||
audioSource =
|
|
||||||
ProgressiveMediaSource.Factory(dataSourceFactory)
|
|
||||||
.createMediaSource(
|
|
||||||
fromUri(
|
|
||||||
response.audioStreams[
|
|
||||||
getMostBitRate(
|
|
||||||
response.audioStreams
|
|
||||||
)
|
|
||||||
].url!!
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
val mergeSource: MediaSource =
|
|
||||||
MergingMediaSource(videoSource, audioSource)
|
|
||||||
exoPlayer.setMediaSource(mergeSource)
|
|
||||||
}
|
}
|
||||||
exoPlayer.seekTo(lastPosition)
|
exoPlayer.seekTo(lastPosition)
|
||||||
view.findViewById<TextView>(R.id.quality_text).text =
|
view.findViewById<TextView>(R.id.quality_text).text = videosNameArray[which]
|
||||||
videosNameArray[which]
|
|
||||||
}
|
}
|
||||||
val dialog = builder.create()
|
val dialog = builder.create()
|
||||||
dialog.show()
|
dialog.show()
|
||||||
|
@ -667,4 +667,9 @@
|
|||||||
<item>450</item>
|
<item>450</item>
|
||||||
</string-array>
|
</string-array>
|
||||||
|
|
||||||
|
<string-array name="playerVideoFormats">
|
||||||
|
<item>WEBM</item>
|
||||||
|
<item>MPEG_4</item>
|
||||||
|
</string-array>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
@ -183,4 +183,5 @@
|
|||||||
<string name="hide_chapters">Hide chapters</string>
|
<string name="hide_chapters">Hide chapters</string>
|
||||||
<string name="buffering_goal">Buffering goal</string>
|
<string name="buffering_goal">Buffering goal</string>
|
||||||
<string name="buffering_goal_summary">The amount of seconds videos get preloaded at maximum.</string>
|
<string name="buffering_goal_summary">The amount of seconds videos get preloaded at maximum.</string>
|
||||||
|
<string name="playerVideoFormat">Player video format</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -13,6 +13,15 @@
|
|||||||
app:title="@string/defres"
|
app:title="@string/defres"
|
||||||
app:useSimpleSummaryProvider="true" />
|
app:useSimpleSummaryProvider="true" />
|
||||||
|
|
||||||
|
<ListPreference
|
||||||
|
android:icon="@drawable/ic_hd"
|
||||||
|
app:defaultValue="WEBM"
|
||||||
|
app:entries="@array/playerVideoFormats"
|
||||||
|
app:entryValues="@array/playerVideoFormats"
|
||||||
|
app:key="player_video_format"
|
||||||
|
app:title="@string/playerVideoFormat"
|
||||||
|
app:useSimpleSummaryProvider="true" />
|
||||||
|
|
||||||
<ListPreference
|
<ListPreference
|
||||||
android:icon="@drawable/ic_play"
|
android:icon="@drawable/ic_play"
|
||||||
app:defaultValue="1F"
|
app:defaultValue="1F"
|
||||||
@ -28,8 +37,8 @@
|
|||||||
app:entries="@array/bufferingGoal"
|
app:entries="@array/bufferingGoal"
|
||||||
app:entryValues="@array/bufferingGoal"
|
app:entryValues="@array/bufferingGoal"
|
||||||
app:key="buffering_goal"
|
app:key="buffering_goal"
|
||||||
app:title="@string/buffering_goal"
|
app:summary="@string/buffering_goal_summary"
|
||||||
app:summary="@string/buffering_goal_summary" />
|
app:title="@string/buffering_goal" />
|
||||||
|
|
||||||
</PreferenceCategory>
|
</PreferenceCategory>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user