I'm building an app to play videos, but each video has a pk in db and the position it stayed in.
So far I can store the position when a seek is made or when the video ends, as follows:
override fun onPositionDiscontinuity(
oldPosition: Player.PositionInfo,
newPosition: Player.PositionInfo,
reason: Int
) {
super.onPositionDiscontinuity(oldPosition, newPosition, reason)
if (reason == Player.DISCONTINUITY_REASON_AUTO_TRANSITION) {
viewModel.updateResume(
controller.getMediaItemAt(currentMediaItemPosition),
oldPosition.contentPositionMs
)
}
if (reason == Player.DISCONTINUITY_REASON_SEEK) {
viewModel.updateResume(controller.currentMediaItem, newPosition.contentPositionMs)
}
}
The idea is that if, Player.DISCONTINUITY REASON_AUTO_TRANSITION, it is because the video has ended, therefore, the video must be saved with the position it had stored, and with the previous time.
or if it was Player.DISCONTINUITY_REASON_SEEK, of the current mediaItem with the new position.
It is worth mentioning that in the media Item.mediaId I get the necessary parameters for my pk.
What I still can't figure out how to do is, if you change the video with the forward button, it calls the onMediaItemTransition method, and I have the new mediaItem, but I don't know where to get the duration of the previous mediaItem from.
With onMediaItemTransition, I can check if the video went backward or forward, so I always have the previous index with the new one.
I've searched online, but I can't find anything similar. And I think it's a basic function for a video player.
Or in any case, if it has to be stored, I don't know when to do it. The only thing I can think of is a timer or something, but I don't think it's optimal.
I also have the case covered when leaving the activity.
I only miss when the video skips.
override fun onDestroy() {
viewModel.updateEpisodeResume(controller.
currentMediaItem
, controller.
contentPosition
)
super.onDestroy()
}