MPVPlayer: Improve track selection and some code cleanup

This commit is contained in:
jarnedemeulemeester 2021-09-17 11:55:35 +02:00
parent 87edeabb9a
commit 0baef5ec2e
No known key found for this signature in database
GPG key ID: B61B7B150DB6A6D2
4 changed files with 59 additions and 33 deletions

View file

@ -48,6 +48,12 @@ class PlayerActivity : AppCompatActivity() {
val audioButton = binding.playerView.findViewById<ImageButton>(R.id.btn_audio_track) val audioButton = binding.playerView.findViewById<ImageButton>(R.id.btn_audio_track)
val subtitleButton = binding.playerView.findViewById<ImageButton>(R.id.btn_subtitle) val subtitleButton = binding.playerView.findViewById<ImageButton>(R.id.btn_subtitle)
audioButton.isEnabled = false
audioButton.imageAlpha = 75
subtitleButton.isEnabled = false
subtitleButton.imageAlpha = 75
audioButton.setOnClickListener { audioButton.setOnClickListener {
when (viewModel.player) { when (viewModel.player) {
is MPVPlayer -> { is MPVPlayer -> {
@ -110,6 +116,15 @@ class PlayerActivity : AppCompatActivity() {
} }
} }
viewModel.fileLoaded.observe(this, {
if (it) {
audioButton.isEnabled = true
audioButton.imageAlpha = 255
subtitleButton.isEnabled = true
subtitleButton.imageAlpha = 255
}
})
viewModel.navigateBack.observe(this, { viewModel.navigateBack.observe(this, {
if (it) { if (it) {
onBackPressed() onBackPressed()

View file

@ -16,35 +16,45 @@ class TrackSelectionDialogFragment(
val trackNames: List<String> val trackNames: List<String>
when (type) { when (type) {
TrackType.AUDIO -> { TrackType.AUDIO -> {
trackNames = viewModel.currentAudioTracks.map { trackNames = viewModel.currentAudioTracks.map { track ->
if (it.title.isEmpty()) { if (track.title.isEmpty()) {
"${it.lang} - ${it.codec}" "${track.lang} - ${track.codec}"
} else { } else {
"${it.title} - ${it.lang} - ${it.codec}" "${track.title} - ${track.lang} - ${track.codec}"
} }
} }
return activity?.let { return activity?.let { activity ->
val builder = AlertDialog.Builder(it) val builder = AlertDialog.Builder(activity)
builder.setTitle("Select audio track") builder.setTitle("Select audio track")
.setItems(trackNames.toTypedArray()) { _, which -> .setSingleChoiceItems(
viewModel.switchToTrack(TrackType.AUDIO, viewModel.currentAudioTracks[which]) trackNames.toTypedArray(),
viewModel.currentAudioTracks.indexOfFirst { it.selected }) { _, which ->
viewModel.switchToTrack(
TrackType.AUDIO,
viewModel.currentAudioTracks[which]
)
} }
builder.create() builder.create()
} ?: throw IllegalStateException("Activity cannot be null") } ?: throw IllegalStateException("Activity cannot be null")
} }
TrackType.SUBTITLE -> { TrackType.SUBTITLE -> {
trackNames = viewModel.currentSubtitleTracks.map { trackNames = viewModel.currentSubtitleTracks.map { track ->
if (it.title.isEmpty()) { if (track.title.isEmpty()) {
"${it.lang} - ${it.codec}" "${track.lang} - ${track.codec}"
} else { } else {
"${it.title} - ${it.lang} - ${it.codec}" "${track.title} - ${track.lang} - ${track.codec}"
} }
} }
return activity?.let { return activity?.let { activity ->
val builder = AlertDialog.Builder(it) val builder = AlertDialog.Builder(activity)
builder.setTitle("Select subtitle track") builder.setTitle("Select subtitle track")
.setItems(trackNames.toTypedArray()) { _, which -> .setSingleChoiceItems(
viewModel.switchToTrack(TrackType.SUBTITLE, viewModel.currentSubtitleTracks[which]) trackNames.toTypedArray(),
viewModel.currentSubtitleTracks.indexOfFirst { it.selected }) { _, which ->
viewModel.switchToTrack(
TrackType.SUBTITLE,
viewModel.currentSubtitleTracks[which]
)
} }
builder.create() builder.create()
} ?: throw IllegalStateException("Activity cannot be null") } ?: throw IllegalStateException("Activity cannot be null")

View file

@ -1285,7 +1285,8 @@ class MPVPlayer(
COMMAND_GET_MEDIA_ITEMS_METADATA, COMMAND_GET_MEDIA_ITEMS_METADATA,
COMMAND_CHANGE_MEDIA_ITEMS, COMMAND_CHANGE_MEDIA_ITEMS,
COMMAND_SET_VIDEO_SURFACE, COMMAND_SET_VIDEO_SURFACE,
COMMAND_SEEK_FORWARD COMMAND_SEEK_FORWARD,
COMMAND_SEEK_BACK
) )
.build() .build()

View file

@ -39,6 +39,9 @@ constructor(
var currentAudioTracks: MutableList<MPVPlayer.Companion.Track> = mutableListOf() var currentAudioTracks: MutableList<MPVPlayer.Companion.Track> = mutableListOf()
var currentSubtitleTracks: MutableList<MPVPlayer.Companion.Track> = mutableListOf() var currentSubtitleTracks: MutableList<MPVPlayer.Companion.Track> = mutableListOf()
private val _fileLoaded = MutableLiveData(false)
val fileLoaded: LiveData<Boolean> = _fileLoaded
private var items: Array<PlayerItem> = arrayOf() private var items: Array<PlayerItem> = arrayOf()
val trackSelector = DefaultTrackSelector(application) val trackSelector = DefaultTrackSelector(application)
@ -96,18 +99,19 @@ constructor(
Timber.e(e) Timber.e(e)
} }
val useMpv = sp.getBoolean("mpv_player", false) when (player) {
is MPVPlayer -> {
if (useMpv) {
player.setMediaItem(mediaItems[0]) player.setMediaItem(mediaItems[0])
player.prepare() player.prepare()
player.play() player.play()
} else { }
is SimpleExoPlayer -> {
player.setMediaItems(mediaItems, currentWindow, items[0].playbackPosition) player.setMediaItems(mediaItems, currentWindow, items[0].playbackPosition)
player.playWhenReady = playWhenReady player.playWhenReady = playWhenReady
player.prepare() player.prepare()
} }
} }
}
pollPosition(player) pollPosition(player)
} }
@ -198,10 +202,8 @@ constructor(
} }
} }
} }
is SimpleExoPlayer -> {
Timber.d(player.currentTrackGroups.length.toString())
}
} }
_fileLoaded.value = true
} }
ExoPlayer.STATE_ENDED -> { ExoPlayer.STATE_ENDED -> {
stateString = "ExoPlayer.STATE_ENDED -" stateString = "ExoPlayer.STATE_ENDED -"
@ -220,8 +222,6 @@ constructor(
fun switchToTrack(trackType: String, track: MPVPlayer.Companion.Track) { fun switchToTrack(trackType: String, track: MPVPlayer.Companion.Track) {
if (player is MPVPlayer) { if (player is MPVPlayer) {
player.selectTrack(trackType, isExternal = false, index = track.ffIndex) player.selectTrack(trackType, isExternal = false, index = track.ffIndex)
} else if (player is SimpleExoPlayer) {
} }
} }
} }