attempt to set language by locale

This commit is contained in:
Bnyro 2022-11-16 13:59:54 +01:00
parent bc9110ed27
commit 4ac05e5171
3 changed files with 36 additions and 24 deletions

View File

@ -108,6 +108,7 @@ import kotlinx.coroutines.launch
import org.chromium.net.CronetEngine
import retrofit2.HttpException
import java.io.IOException
import java.util.*
import java.util.concurrent.Executors
import kotlin.math.abs
@ -167,6 +168,9 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
private lateinit var shareData: ShareData
private var selectedAudioSourceUrl: String? = null
private var selectedVideoSourceUrl: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
@ -1068,7 +1072,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
}
private fun setMediaSource(
videoUri: Uri,
videoUrl: String,
audioUrl: String
) {
val checkIntervalSize = when (PlayerHelper.progressiveLoadingIntervalSize) {
@ -1080,7 +1084,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
DefaultHttpDataSource.Factory()
val videoItem: MediaItem = MediaItem.Builder()
.setUri(videoUri)
.setUri(videoUrl.toUri())
.setSubtitleConfigurations(subtitles)
.build()
@ -1107,16 +1111,16 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
exoPlayer.setMediaItem(mediaItem)
}
private fun getAvailableResolutions(): Pair<Array<String>, Array<Uri>> {
private fun getAvailableResolutions(): Pair<Array<String>, Array<String>> {
if (!this::streams.isInitialized) return Pair(arrayOf(), arrayOf())
var videosNameArray: Array<String> = arrayOf()
var videosUrlArray: Array<Uri> = arrayOf()
var videosUrlArray: Array<String> = arrayOf()
// append hls to list if available
if (streams.hls != null) {
videosNameArray += getString(R.string.hls)
videosUrlArray += streams.hls!!.toUri()
videosUrlArray += streams.hls!!
}
val videoStreams = try {
@ -1139,10 +1143,10 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
val preferredMimeType = "video/${PlayerHelper.videoFormatPreference}"
if (vid.url != null && vid.mimeType == preferredMimeType) { // preferred format
videosNameArray += vid.quality.toString()
videosUrlArray += vid.url!!.toUri()
videosUrlArray += vid.url!!
} else if (vid.quality.equals("LBRY") && vid.format.equals("MP4")) { // LBRY MP4 format
videosNameArray += "LBRY MP4"
videosUrlArray += vid.url!!.toUri()
videosUrlArray += vid.url!!
}
}
return Pair(videosNameArray, videosUrlArray)
@ -1190,17 +1194,16 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
private fun setStreamSource(
streams: Streams,
videosNameArray: Array<String>,
videosUrlArray: Array<Uri>
videosUrlArray: Array<String>
) {
val defaultResolution = PlayerHelper.getDefaultResolution(requireContext())
if (defaultResolution != "") {
videosNameArray.forEachIndexed { index, pipedStream ->
// search for quality preference in the available stream sources
if (pipedStream.contains(defaultResolution)) {
val videoUri = videosUrlArray[index]
val audioUrl =
PlayerHelper.getAudioSource(requireContext(), streams.audioStreams!!)
setMediaSource(videoUri, audioUrl)
selectedVideoSourceUrl = videosUrlArray[index]
selectedAudioSourceUrl = selectedAudioSourceUrl ?: getAudioSource(streams.audioStreams)
setMediaSource(selectedAudioSourceUrl!!, selectedVideoSourceUrl!!)
return
}
}
@ -1220,6 +1223,15 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
}
}
private fun getAudioSource(audioStreams: List<PipedStream>?): String {
val appLanguage = Locale.getDefault().language.lowercase().substring(0, 2)
val filteredStreams = audioStreams.orEmpty().filter { it.audioTrackId?.contains(appLanguage) ?: false }
return PlayerHelper.getAudioSource(
requireContext(),
filteredStreams.ifEmpty { audioStreams!! }
)
}
private fun createExoPlayer() {
val cronetEngine: CronetEngine = CronetHelper.getCronetEngine()
val cronetDataSourceFactory: CronetDataSource.Factory =
@ -1396,33 +1408,32 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
videosNameArray[which] == "LBRY HLS"
) {
// set the progressive media source
setHLSMediaSource(videosUrlArray[which])
setHLSMediaSource(videosUrlArray[which].toUri())
} else {
val videoUri = videosUrlArray[which]
val audioUrl =
PlayerHelper.getAudioSource(requireContext(), streams.audioStreams!!)
setMediaSource(videoUri, audioUrl)
selectedVideoSourceUrl = videosUrlArray[which]
selectedAudioSourceUrl= selectedAudioSourceUrl ?: getAudioSource(streams.audioStreams)
setMediaSource(selectedVideoSourceUrl!!, selectedAudioSourceUrl!!)
}
exoPlayer.seekTo(lastPosition)
}
.show(childFragmentManager)
}
fun getAudioStreamGroups(audioStreams: List<PipedStream>?): Map<String, List<PipedStream>> {
private fun getAudioStreamGroups(audioStreams: List<PipedStream>?): Map<String?, List<PipedStream>> {
return audioStreams.orEmpty()
.filter { it.audioTrackName != null }
.groupBy { it.audioTrackName!! }
.groupBy { it.audioTrackName }
}
override fun onAudioStreamClicked() {
val audioGroups = getAudioStreamGroups(streams.audioStreams)
val audioLanguages = audioGroups.map { it.key }
val audioLanguages = audioGroups.map { it.key ?: getString(R.string.default_audio_track) }
BaseBottomSheet()
.setSimpleItems(audioLanguages) { index ->
val audioStreams = audioGroups.values.elementAt(index)
val audioUrl = PlayerHelper.getAudioSource(requireContext(), audioStreams)
setMediaSource(streams.videoStreams!!.first().url!!.toUri(), audioUrl)
selectedAudioSourceUrl = PlayerHelper.getAudioSource(requireContext(), audioStreams)
selectedVideoSourceUrl = selectedVideoSourceUrl ?: streams.videoStreams!!.first().url!!
setMediaSource(selectedAudioSourceUrl!!, selectedVideoSourceUrl!!)
}
.show(childFragmentManager)
}

View File

@ -198,7 +198,7 @@ internal class CustomExoPlayerView(
items.add(
BottomSheetItem(
context.getString(R.string.audio_track),
R.drawable.ic_audio,
R.drawable.ic_audio
) {
playerOptionsInterface?.onAudioStreamClicked()
}

View File

@ -371,6 +371,7 @@
<string name="alternative_player_layout">Alternative player layout</string>
<string name="alternative_player_layout_summary">Show the related videos as a row above the comments instead of below.</string>
<string name="audio_track">Audio track</string>
<string name="default_audio_track">Default</string>
<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string>