Exoplayer: onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) is getting called twice on next button click event

Created on 28 Jan 2019  路  7Comments  路  Source: google/ExoPlayer

Issue description:

While migrating my player from ExoPlayer1 to ExoPlayer2, I have a use case in which I need to set some data specific to the current media (of the playlist), at the start of its playback. After going through ExoPlayer2 docs I found two possible callbacks (event listener of exoPlayer2):

  1. onPositionDiscontinuity(int reason)
    But as this callback also fired in case of seek so I can not use this.
  1. onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections)
    This method seems working for me as it gets called when available/selected track changed. It was working perfectly for me for exoPlayerV2.6.3 to exoPlayerV2.8.4, But as I moved to the latest ExoPlayer2 version (V2.9.4) I observed that
  2. onTracksChanged() is getting called single time, as expected, When current media of the playlist completes its playback and next media loaded.
  3. onTracksChanged() is getting called twice on"next" button clicked with available tracks length (TrackGroupArray ) as 0 (always) on 1st call and then on 2nd call with available tracks length greater than 0 or more as per its content.
    And thus my logic of setting data for the current playing media is failing.
    SO, it would be great if I can get clarity on below:
  4. Is there any other callbacks which get fired (single time) when current window index changed (on start of each media playback for the playlist) ?
  5. Why onTracksChanged() is firing twice on "next" button click event.

Device used: Moto E4+

Android Version: Android Api 7.1

Reproduction steps:

   1. Create a sample with ExoPlayerV2.9.+ 
   2. Create ConcatenatingMediaSource with 2 or more media urls
   3. Start the player and click "next" button from the available exoPlayer2 controls.
question

All 7 comments

Please have a look at our FAQs about this question. Or look at similar issues which go in a bit more detail (e.g. #4898)

Please have a look at our FAQs about this question. Or look at similar issues which go in a bit more detail (e.g. #4898)

Gone through the FAQs, But for the playlist:

  • on normal media playback end and start of next media playback : reason = Player.DISCONTINUITY_REASON_PERIOD_TRANSITION. and I am also getting the same. BUT
  • on next button click why I am getting below two reason one after another ?:
    reason = Player.DISCONTINUITY_REASON_SEEK
    reason = Player.DISCONTINUITY_REASON_SEEK_ADJUSTMENT

If you are seeking to the next playlist item (that is, click on the next button), you get the discontinuity with reason SEEK. If the next playlist item needs to adjust the position for some reason, you'll get another discontinuity with reason SEEK_ADJUSTMENT because the position changed again.

Thanks for the quick help 馃憤. Now I am moving my track change logic from "onTrackChanged()" to onPositionDiscontinuity().
Just one more query, Will the track change logic keep working correctly with "onTrackChanged()" for Exoplayer version < 2.9.0 ?

onTracksChanged actually had a bug < 2.9.0 because it only reported the one change instead of two. Note that the "tracks" which are changed are unrelated to playlist items. Tracks are specific selections of video, audio and text tracks. If you seek to new playlist item which hasn't been prepared yet, the player briefly transitions to a state without selected tracks and shortly after that it selects tracks for the new item as soon as all required information is available.

@tonihei
on next button click why I am getting below two reason one after another :
reason = Player.DISCONTINUITY_REASON_SEEK
reason = Player.DISCONTINUITY_REASON_SEEK_ADJUSTMENT
reason = Player.DISCONTINUITY_REASON_SEEK

also when I seek,
reason = Player.DISCONTINUITY_REASON_SEEK
gets called.
Not sure where should how can I check track changed

@ummerakbar45
_> Not sure where should how can I check track changed_
When playback moves from one item to the next, EventListener.onPositionDiscontinuity will be called with reason = Player.DISCONTINUITY_REASON_PERIOD_TRANSITION
Query the player to determine which item in the playlist is now being played using:
Player.getCurrentWindowIndex and Player.getCurrentTag and the determine if the track gets changed in the playlist.

Was this page helpful?
0 / 5 - 0 ratings