Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Commit

Permalink
feat: add audio tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
kuylar committed Aug 30, 2023
1 parent 64a9875 commit 61c129b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class PlayerSettingsFragment(
loadQualityMenu()
loadSubtitleMenu()
loadSpeedMenu()
loadAudioTracksMenu()
binding.playerSettingsLoopValue.text =
if (player.repeatMode == Player.REPEAT_MODE_ONE) getString(R.string.on)
else getString(R.string.off)
Expand All @@ -54,7 +55,7 @@ class PlayerSettingsFragment(
}
binding.playerSettingsButtonAudio.setOnClickListener {
binding.playerSettingsMain.visibility = View.GONE
binding.playerSettingsTrack.visibility = View.VISIBLE
binding.playerSettingsAudio.visibility = View.VISIBLE
}

when (defaultScreen) {
Expand Down Expand Up @@ -219,4 +220,43 @@ class PlayerSettingsFragment(
else -> getString(R.string.player_speed_template, speed.toString())
}
}

private fun loadAudioTracksMenu() {
val audioTracks = player.currentTracks.groups.filter { it.type == C.TRACK_TYPE_AUDIO }
if (audioTracks.size > 1) {
val params =
player.trackSelectionParameters.overrides.filter { it.key.type == C.TRACK_TYPE_AUDIO }

binding.playerSettingsAudioValue.text = if (params.isEmpty()) {
getString(R.string.off)
} else {
val f =
audioTracks
.find { it.mediaTrackGroup.id == params.values.first()!!.mediaTrackGroup.id }
?.getTrackFormat(0)
f?.label ?: f?.id ?: f?.language ?: getString(R.string.unavailable)
}

for (group in audioTracks) {
val index = group.length - 1 // select the HQ one
val f = group.getTrackFormat(index)
val item = createMenuItem(
f.label ?: f.id ?: f.language ?: getString(R.string.unavailable),
if (params.isNotEmpty()) params.values.first()!!.mediaTrackGroup.id == group.mediaTrackGroup.id else false
) {
player.trackSelectionParameters = player.trackSelectionParameters
.buildUpon()
.clearOverridesOfType(C.TRACK_TYPE_AUDIO)
.setOverrideForType(
TrackSelectionOverride(group.mediaTrackGroup, index)
)
.build()
dismissNow()
}
binding.playerSettingsAudio.addView(item)
}
} else {
binding.playerSettingsButtonAudio.visibility = View.GONE
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/layout/fragment_player_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
android:text="@string/bullet" />

<TextView
android:id="@+id/player_settings_track_value"
android:id="@+id/player_settings_audio_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/unavailable" />
Expand Down Expand Up @@ -221,7 +221,7 @@
android:visibility="gone" />

<LinearLayout
android:id="@+id/player_settings_track"
android:id="@+id/player_settings_audio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
Expand Down

0 comments on commit 61c129b

Please sign in to comment.