skip to Main Content

I have tried the code from exoplayer documentation, it works but when I fetch selected track it shows the default track which I already changed.

player.setTrackSelectionParameters(
    player.getTrackSelectionParameters()
        .buildUpon()
        .setOverrideForType(
            new TrackSelectionOverride(
                audioTrackGroup.getMediaTrackGroup(),
                /* trackIndex= */ 0))
        .build());

The code I used before the latest release is:

    private fun setSubTrack(
        trackSelector: DefaultTrackSelector?,
        subLanguage: String
    ) {
        trackSelector?.setParameters(trackSelector.buildUponParameters()
            .setRendererDisabled(C.TRACK_TYPE_VIDEO, false)
            .setPreferredTextLanguage(subLanguage))
    }

This code worked perfectly. But from the latest release this code is not working. So i changed this with the above one.

My entire code to change tracks:

package ysports.app.player

import android.content.Context
import android.content.Intent
import android.net.Uri
import com.google.android.exoplayer2.*
import com.google.android.exoplayer2.trackselection.TrackSelectionOverride
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.common.collect.ImmutableList
import ysports.app.PlayerActivity
import ysports.app.R
import ysports.app.api.fixture.Media
import java.util.*

class PlayerUtil {

    private var selectedAudioPosition = 0
    private var selectedAudioChoice = 0
    private var selectedSubPosition = 0
    private var selectedSubChoice = 0
    private var selectedVideoPosition = 1
    private var selectedVideoChoice = 1
    private var selectedSpeedPosition = 3
    private var selectedSpeedChoice = 3
    private var isVideoTrackAuto = true

    fun selectAudioTrack(
        context: Context,
        player: ExoPlayer?
    ) {
        val tracks = player?.currentTracks ?: return
        val audioTrackGroups = ArrayList<Tracks.Group>()
        val audioList = ArrayList<String>()

        audioList.add("Disable")

        for (trackGroup in tracks.groups) {
            // Group level information.
            val trackType: Int = trackGroup.type
            //val trackInGroupIsSelected = trackGroup.isSelected
            //val trackInGroupIsSupported = trackGroup.isSupported

            if (trackType == C.TRACK_TYPE_AUDIO) {
                audioTrackGroups.add(trackGroup)
                for (i in 0 until trackGroup.length) {
                    // Individual track information.
                    val isSupported = trackGroup.isTrackSupported(i)
                    val isSelected = trackGroup.isTrackSelected(i)
                    val trackFormat = trackGroup.getTrackFormat(i)

                    if (isSelected) {
                        selectedAudioPosition = i + 1
                        selectedAudioChoice = selectedAudioPosition
                    }
                    var label = if (trackFormat.label != null) " - ${trackFormat.label}" else ""
                    label += if (!isSupported) " (Unsupported)" else ""
                    val list = Locale(trackFormat.language.toString()).displayLanguage + label

                    if (list.contains("und") || list.contains("null")) audioList.add("Audio track #${i + 1}") else audioList.add(list)
                }
            }
        }

        val tempTracks = audioList.toArray(arrayOfNulls<CharSequence>(audioList.size))
        MaterialAlertDialogBuilder(context)
            .setTitle("Audio Track")
            .setNeutralButton(context.getString(R.string.cancel)) { _, _ -> }
            .setPositiveButton(context.getString(R.string.ok)) { _, _ ->
                selectedAudioPosition = selectedAudioChoice
                if (selectedAudioPosition == 0) {
                    setAudioTrack(player, audioTrackGroups[0], true)
                } else {
                    setAudioTrack(player, audioTrackGroups[selectedAudioPosition - 1], false)
                }
            }
            .setSingleChoiceItems(tempTracks, selectedAudioPosition) { _, position ->
                selectedAudioChoice = position
            }
            .show()
    }

    private fun setAudioTrack(
        player: ExoPlayer?,
        audioTrackGroup: Tracks.Group?,
        audioDisabled: Boolean
    ) {
        player?.trackSelectionParameters = player?.trackSelectionParameters!!
            .buildUpon()
            .setTrackTypeDisabled(C.TRACK_TYPE_AUDIO, audioDisabled)
            .setOverrideForType(
                TrackSelectionOverride(
                    audioTrackGroup?.mediaTrackGroup!!, 0
                )
            )
            .build()
    }

    fun selectSubTrack(
        context: Context,
        player: ExoPlayer?
    ) {
        val tracks = player?.currentTracks ?: return
        val textTrackGroups = ArrayList<Tracks.Group>()
        val subtitlesList = ArrayList<String>()

        subtitlesList.add("Disable")

        for (trackGroup in tracks.groups) {
            // Group level information.
            val trackType: Int = trackGroup.type
            //val trackInGroupIsSelected = trackGroup.isSelected
            //val trackInGroupIsSupported = trackGroup.isSupported

            if (trackType == C.TRACK_TYPE_TEXT) {
                textTrackGroups.add(trackGroup)
                for (i in 0 until trackGroup.length) {
                    // Individual track information.
                    val isSupported = trackGroup.isTrackSupported(i)
                    val isSelected = trackGroup.isTrackSelected(i)
                    val trackFormat = trackGroup.getTrackFormat(i)

                    if (isSelected) {
                        selectedSubPosition = i + 1
                        selectedSubChoice = selectedSubPosition
                    }
                    var label = if (trackFormat.label != null) " - ${trackFormat.label}" else ""
                    label += if (!isSupported) " (Unsupported)" else ""

                    subtitlesList.add(Locale(trackFormat.language.toString()).displayLanguage + label)
                }
            }
        }

        val tempTracks = subtitlesList.toArray(arrayOfNulls<CharSequence>(subtitlesList.size))
        MaterialAlertDialogBuilder(context)
            .setTitle("Subtitle")
            .setNeutralButton(context.getString(R.string.cancel)) { _, _ -> }
            .setPositiveButton(context.getString(R.string.ok)) { _, _ ->
                selectedSubPosition = selectedSubChoice
                if (selectedSubPosition == 0) {
                    setSubTrack(player, textTrackGroups[0], true)
                } else {
                    setSubTrack(player, textTrackGroups[selectedSubPosition - 1], false)
                }
            }
            .setSingleChoiceItems(tempTracks, selectedSubPosition) { _, position ->
                selectedSubChoice = position
            }
            .show()
    }

    private fun setSubTrack(
        player: ExoPlayer?,
        textTrackGroup: Tracks.Group?,
        textDisabled: Boolean
    ) {
        player?.trackSelectionParameters = player?.trackSelectionParameters!!
            .buildUpon()
            .setTrackTypeDisabled(C.TRACK_TYPE_TEXT, textDisabled)
            .setOverrideForType(
                TrackSelectionOverride(
                    textTrackGroup?.mediaTrackGroup!!, 0
                )
            )
            .build()
    }

    fun selectVideoTrack(
        context: Context,
        player: ExoPlayer?
    ) {
        val tracks = player?.currentTracks ?: return

        var group: Tracks.Group? = null
        val videoList = ArrayList<String>()

        videoList.add("Disable")
        videoList.add("Auto")

        for (trackGroup in tracks.groups) {
            // Group level information.
            val trackType: Int = trackGroup.type
            //val trackInGroupIsSelected = trackGroup.isSelected
            //val trackInGroupIsSupported = trackGroup.isSupported

            if (trackType == C.TRACK_TYPE_VIDEO) {
                group = trackGroup
                for (i in 0 until trackGroup.length) {
                    // Individual track information.
                    val isSupported = trackGroup.isTrackSupported(i)
                    val isSelected = trackGroup.isTrackSelected(i)
                    val trackFormat = trackGroup.getTrackFormat(i)

                    if (isSelected) {
                        selectedVideoPosition = i + 2
                        selectedVideoChoice = selectedVideoPosition
                    }
                    if (isVideoTrackAuto) {
                        selectedVideoPosition = 1
                        selectedVideoChoice = 1
                    }
                    var bitrate = ""
                    if (trackFormat.bitrate != -1) {
                        bitrate = String.format("- %.2f Mbps", trackFormat.bitrate * 0.000001)
                    }
                    val width = trackFormat.width
                    val height = trackFormat.height
                    val list = if (isSupported) "${width}x$height $bitrate" else "${width}x$height $bitrate (Unsupported)"

                    videoList.add(list)
                }
            }
        }

        val tempTracks = videoList.toArray(arrayOfNulls<CharSequence>(videoList.size))
        MaterialAlertDialogBuilder(context)
            .setTitle("Quality")
            .setNeutralButton(context.getString(R.string.cancel)) { _, _ -> }
            .setPositiveButton(context.getString(R.string.ok)) { _, _ ->
                selectedVideoPosition = selectedVideoChoice
                isVideoTrackAuto = false
                setVideoTrack(player, group, selectedVideoPosition)
            }
            .setSingleChoiceItems(tempTracks, selectedVideoPosition) { _, position ->
                selectedVideoChoice = position
            }
            .show()
    }

    private fun setVideoTrack(
        player: ExoPlayer?,
        videoTrackGroup: Tracks.Group?,
        trackIndex: Int
    ) {
        when (selectedVideoPosition) {
            0 -> {
                player?.trackSelectionParameters = player?.trackSelectionParameters!!
                    .buildUpon()
                    .setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, true)
                    .build()
            }
            1 -> {
                isVideoTrackAuto = true
                player?.trackSelectionParameters = player?.trackSelectionParameters!!
                    .buildUpon()
                    .setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, false)
                    .setMaxVideoSizeSd()
                    .build()
            }
            else -> {
                player?.trackSelectionParameters = player?.trackSelectionParameters!!
                    .buildUpon()
                    .setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, false)
                    .setOverrideForType(
                        TrackSelectionOverride(
                            videoTrackGroup?.mediaTrackGroup!!, trackIndex - 2
                        )
                    )
                    .build()
            }
        }
    }

    fun setPlaybackSpeed(
        context: Context,
        player: ExoPlayer?
    ) {
        if (player == null) return
        val speedList = arrayOf("0.25", "0.5", "0.75", "Normal", "1.25", "1.5", "1.75", "2")
        val speed = floatArrayOf(0.25F, 0.5F, 0.75F, 1F, 1.25F, 1.5F, 1.75F, 2F)

        MaterialAlertDialogBuilder(context)
            .setTitle(context.getString(R.string.playback_speed))
            .setNeutralButton(context.getString(R.string.cancel)) { _, _ -> }
            .setPositiveButton(context.getString(R.string.ok)) { _, _ ->
                selectedSpeedPosition = selectedSpeedChoice
                player.setPlaybackSpeed(speed[selectedSpeedPosition])
            }
            .setSingleChoiceItems(speedList, selectedSpeedPosition) { _, position ->
                selectedSpeedChoice = position
            }
            .show()
    }
}

This code changes the audio, video and text tracks but val isSelected = trackGroup.isTrackSelected(i) is always return the default track.

Anyone know how to fix this problem!

2

Answers


  1. I am also struggling like you and trying to achieve the same thing.
    So far I have found these..this is working but also had some problems if there are multiple trackGroups of the same type. e.g: audio

    player?.let {
                        /*it.trackSelectionParameters = it.trackSelectionParameters
                            .buildUpon()
                            .setPreferredAudioLanguage(audioTracks[selectedAudioTrack])
                            .build()*/
    
                        it.trackSelectionParameters = it.trackSelectionParameters
                            .buildUpon()
                            .setTrackSelectionOverrides(
                                TrackSelectionOverrides
                                    .Builder()
                                    .addOverride(TrackSelectionOverrides.TrackSelectionOverride(
                                        audioTrackGroup!!, listOf(selectedAudioTrack)
                                    ))
                                    .build()
                            )
                            .build()
                    }
    
    Login or Signup to reply.
  2. This should work.

    private fun selectAudioTrack() {
        val tracks = player?.currentTracks ?: return
        val audioTrackGroups = ArrayList<Tracks.Group>()
        val audioList = ArrayList<String>()
        audioList.add("None")
        var selected = 0
    
        for (trackGroup in tracks.groups) {
            // Group level information.
            val trackType: Int = trackGroup.type
            //val trackInGroupIsSelected = trackGroup.isSelected
            //val trackInGroupIsSupported = trackGroup.isSupported
    
            if (trackType == C.TRACK_TYPE_AUDIO) {
                audioTrackGroups.add(trackGroup)
                for (i in 0 until trackGroup.length) {
                    // Individual track information.
                    val isSupported = trackGroup.isTrackSupported(i)
                    val isSelected = trackGroup.isTrackSelected(i)
                    val trackFormat = trackGroup.getTrackFormat(i)
                    selected++
                    if (isSelected) {
                        selectedAudioPosition = selected
                    }
                    Log.d("track", "$isSupported $isSelected $trackFormat $selectedAudioPosition")
    
                    var label = if (trackFormat.label != null) " - ${trackFormat.label}(${
                        formatNameFromMime(trackFormat.sampleMimeType)
                    })" else ""
                    label += if (!isSupported) " (Unsupported)" else ""
                    val list = Locale(trackFormat.language.toString()).displayLanguage + label
    
                    if (list.contains("und") || list.contains("null")) audioList.add("Audio track #${i + 1}") else audioList.add(
                        list
                    )
                }
            }
        }
    
        
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search