Right way to clear combined PlaybackStats without releasing the player
- Dominant language
- Java
- Stars
- 21.9k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
I am not sure if this is a bug or I am using the `PlaybackStatsListener` incorrectly.
This is how I have been using `PlaybackStats` and `SimpleExoPlayer`.
First init ExoPlayer:
```
mTrackSelector = new DefaultTrackSelector(mContext);
mExoPlayer =
new SimpleExoPlayer.Builder(mContext)
.setTrackSelector(mTrackSelector)
.build();
mExoPlayer.setPriorityTaskManager(priorityTaskManager);
mExoPlayer.addListener(mPlaybackEventListener);
mExoPlayer.addVideoListener(mVideoListener);
mExoPlayer.addTextOutput(mTextOutputListener);
```
Then, each time I open a video I stop the player and then register an analytics listener if not set:
```
mExoPlayer.stop();
if (mPlaybackStatsListener == null) {
mPlaybackStatsListener = new PlaybackStatsListener(false, null);
mExoPlayer.addAnalyticsListener(mPlaybackStatsListener);
}
```
I add the listener after the stop because the `PlaybackStatsListener` class recommends doing it in idle state, before loading new media:
```
final MediaSource mediaSource = MediaUtils.getMediaSource(mContext, mStream);
mExoPlayer.setMediaSource(mediaSource);
mExoPlayer.prepare();
```
When there is a retry, I keep using the same listener (so the play time adds up as well as other stats like the rebuffering count).
But when switching to a different stream, I was "clearing" the playback stats re-attaching the listener:
```
if (mExoPlayer != null) {
mExoPlayer.removeAnalyticsListener(mPlaybackStatsListener);
}
mPlaybackStatsListener.finishAllSessions();
mPlaybackStatsListener = null;
```
**I remove the listener** because `finishAllSessions` is not enough to clear the PlaybackStats for accessing with `mPlaybackStatsListener.getCombinedPlaybackStats()`.
It was working fine from older versions. Re-attaching a new listener when switching streams was giving me the expected values.
But since ExoPlayer 2.12, the `getTotalPlayTimeMs()` gives always a 0 value and does not increase with playback for live streams.
The only way I can fix the playback stats measurement after switching to (or between) live streams (HLS) is releasing the player.
And I don't think that is convenient, as that frees up resources and registers all listeners multiple times.
These are my questions:
1. I see there is a new playlist API. Is it now mandatory to use it in order to take advantage of the `PlaybackStatsListener`?
2. Since I am not using the playlist API, for loading a new media, is it fine to `stop`, `setMediaSource` and then `prepare`?
3. Is there a method for clearing the combined PlaybackStats in the listener, so I don't need to re-attach it?
4. Since ExoPlayer 2.12 re-attaching the listener doesn't even work and I need to release the player to get the right PlaybackStats for live streams. Am I missing something?
5. I wanted to print some logs but the listener class is marked as final. If I create a wrapper class that will not help since the attributes are private and don't have getters. Do you recommend copying the `PlaybackStatsListener` and all related classes to it that are also final?
Thanks in advance for your help.
Contributor guide
Assessment
This issue has not been assessed yet.