@@ -970,7 +970,9 @@ private void addMediaItemsInternal(int index, List<MediaItem> mediaItems) {
970
970
}
971
971
// Add media items to the end of the timeline if the index exceeds the window count.
972
972
index = min (index , playerInfo .timeline .getWindowCount ());
973
- PlayerInfo newPlayerInfo = maskPlaybackInfoForAddedItems (playerInfo , index , mediaItems );
973
+ PlayerInfo newPlayerInfo =
974
+ maskPlayerInfoForAddedItems (
975
+ playerInfo , index , mediaItems , getCurrentPosition (), getContentPosition ());
974
976
@ Nullable
975
977
@ Player .MediaItemTransitionReason
976
978
Integer mediaItemTransitionReason =
@@ -983,8 +985,23 @@ private void addMediaItemsInternal(int index, List<MediaItem> mediaItems) {
983
985
/* mediaItemTransitionReason= */ mediaItemTransitionReason );
984
986
}
985
987
986
- private static PlayerInfo maskPlaybackInfoForAddedItems (
987
- PlayerInfo playerInfo , int index , List <MediaItem > mediaItems ) {
988
+ /**
989
+ * Returns a masking {@link PlayerInfo} for the added {@linkplain MediaItem media items} with the
990
+ * provided information.
991
+ *
992
+ * @param playerInfo The {@link PlayerInfo} that the new masking {@link PlayerInfo} is based on.
993
+ * @param index The index at which the {@linkplain MediaItem media items} are added.
994
+ * @param mediaItems The {@linkplain MediaItem media items} added.
995
+ * @param currentPositionMs The current position in milliseconds.
996
+ * @param currentContentPositionMs The current content position in milliseconds.
997
+ * @return A masking {@link PlayerInfo}.
998
+ */
999
+ private static PlayerInfo maskPlayerInfoForAddedItems (
1000
+ PlayerInfo playerInfo ,
1001
+ int index ,
1002
+ List <MediaItem > mediaItems ,
1003
+ long currentPositionMs ,
1004
+ long currentContentPositionMs ) {
988
1005
Timeline oldTimeline = playerInfo .timeline ;
989
1006
List <Window > newWindows = new ArrayList <>();
990
1007
List <Period > newPeriods = new ArrayList <>();
@@ -1017,6 +1034,8 @@ private static PlayerInfo maskPlaybackInfoForAddedItems(
1017
1034
newTimeline ,
1018
1035
newMediaItemIndex ,
1019
1036
newPeriodIndex ,
1037
+ currentPositionMs ,
1038
+ currentContentPositionMs ,
1020
1039
Player .DISCONTINUITY_REASON_INTERNAL );
1021
1040
}
1022
1041
@@ -1066,7 +1085,14 @@ private void removeMediaItemsInternal(int fromIndex, int toIndex) {
1066
1085
}
1067
1086
boolean wasCurrentItemRemoved =
1068
1087
getCurrentMediaItemIndex () >= fromIndex && getCurrentMediaItemIndex () < toIndex ;
1069
- PlayerInfo newPlayerInfo = maskPlayerInfoForRemovedItems (playerInfo , fromIndex , toIndex );
1088
+ PlayerInfo newPlayerInfo =
1089
+ maskPlayerInfoForRemovedItems (
1090
+ playerInfo ,
1091
+ fromIndex ,
1092
+ toIndex ,
1093
+ /* isReplacingItems= */ false ,
1094
+ getCurrentPosition (),
1095
+ getContentPosition ());
1070
1096
boolean didMediaItemTransitionHappen =
1071
1097
playerInfo .sessionPositionInfo .positionInfo .mediaItemIndex >= fromIndex
1072
1098
&& playerInfo .sessionPositionInfo .positionInfo .mediaItemIndex < toIndex ;
@@ -1082,8 +1108,30 @@ private void removeMediaItemsInternal(int fromIndex, int toIndex) {
1082
1108
: null );
1083
1109
}
1084
1110
1111
+ /**
1112
+ * Returns a masking {@link PlayerInfo} for the removed {@linkplain MediaItem media items} with
1113
+ * the provided information.
1114
+ *
1115
+ * @param playerInfo The {@link PlayerInfo} that the new masking {@link PlayerInfo} is based on.
1116
+ * @param fromIndex The index at which to start removing media items (inclusive).
1117
+ * @param toIndex The index of the first item to be kept (exclusive).
1118
+ * @param isReplacingItems A boolean indicating whether the media items are removed due to
1119
+ * replacing.
1120
+ * @param currentPositionMs The current position in milliseconds. This value will be used in the
1121
+ * new masking {@link PlayerInfo} if the removal of the media items doesn't affect the current
1122
+ * playback position.
1123
+ * @param currentContentPositionMs The current content position in milliseconds. This value will
1124
+ * be used in the new masking {@link PlayerInfo} if the removal of the media items doesn't
1125
+ * affect the current playback position.
1126
+ * @return A masking {@link PlayerInfo}.
1127
+ */
1085
1128
private static PlayerInfo maskPlayerInfoForRemovedItems (
1086
- PlayerInfo playerInfo , int fromIndex , int toIndex ) {
1129
+ PlayerInfo playerInfo ,
1130
+ int fromIndex ,
1131
+ int toIndex ,
1132
+ boolean isReplacingItems ,
1133
+ long currentPositionMs ,
1134
+ long currentContentPositionMs ) {
1087
1135
Timeline oldTimeline = playerInfo .timeline ;
1088
1136
List <Window > newWindows = new ArrayList <>();
1089
1137
List <Period > newPeriods = new ArrayList <>();
@@ -1141,6 +1189,16 @@ private static PlayerInfo maskPlayerInfoForRemovedItems(
1141
1189
newPositionInfo ,
1142
1190
SessionPositionInfo .DEFAULT ,
1143
1191
Player .DISCONTINUITY_REASON_REMOVE );
1192
+ } else if (isReplacingItems ) {
1193
+ newPlayerInfo =
1194
+ maskTimelineAndPositionInfo (
1195
+ playerInfo ,
1196
+ newTimeline ,
1197
+ newMediaItemIndex ,
1198
+ newPeriodIndex ,
1199
+ currentPositionMs ,
1200
+ currentContentPositionMs ,
1201
+ Player .DISCONTINUITY_REASON_REMOVE );
1144
1202
} else {
1145
1203
Window newWindow = newTimeline .getWindow (newMediaItemIndex , new Window ());
1146
1204
long defaultPositionMs = newWindow .getDefaultPositionMs ();
@@ -1182,6 +1240,8 @@ private static PlayerInfo maskPlayerInfoForRemovedItems(
1182
1240
newTimeline ,
1183
1241
newMediaItemIndex ,
1184
1242
newPeriodIndex ,
1243
+ currentPositionMs ,
1244
+ currentContentPositionMs ,
1185
1245
Player .DISCONTINUITY_REASON_REMOVE );
1186
1246
}
1187
1247
@@ -1290,8 +1350,17 @@ private void replaceMediaItemsInternal(int fromIndex, int toIndex, List<MediaIte
1290
1350
return ;
1291
1351
}
1292
1352
toIndex = min (toIndex , playlistSize );
1293
- PlayerInfo newPlayerInfo = maskPlaybackInfoForAddedItems (playerInfo , toIndex , mediaItems );
1294
- newPlayerInfo = maskPlayerInfoForRemovedItems (newPlayerInfo , fromIndex , toIndex );
1353
+ PlayerInfo newPlayerInfo =
1354
+ maskPlayerInfoForAddedItems (
1355
+ playerInfo , toIndex , mediaItems , getCurrentPosition (), getContentPosition ());
1356
+ newPlayerInfo =
1357
+ maskPlayerInfoForRemovedItems (
1358
+ newPlayerInfo ,
1359
+ fromIndex ,
1360
+ toIndex ,
1361
+ /* isReplacingItems= */ true ,
1362
+ getCurrentPosition (),
1363
+ getContentPosition ());
1295
1364
boolean wasCurrentItemReplaced =
1296
1365
playerInfo .sessionPositionInfo .positionInfo .mediaItemIndex >= fromIndex
1297
1366
&& playerInfo .sessionPositionInfo .positionInfo .mediaItemIndex < toIndex ;
@@ -2111,6 +2180,8 @@ private void moveMediaItemsInternal(int fromIndex, int toIndex, int newIndex) {
2111
2180
newTimeline ,
2112
2181
newWindowIndex ,
2113
2182
newPeriodIndex ,
2183
+ getCurrentPosition (),
2184
+ getContentPosition (),
2114
2185
Player .DISCONTINUITY_REASON_INTERNAL );
2115
2186
2116
2187
updatePlayerInfo (
@@ -2972,6 +3043,8 @@ private static PlayerInfo maskTimelineAndPositionInfo(
2972
3043
Timeline timeline ,
2973
3044
int newMediaItemIndex ,
2974
3045
int newPeriodIndex ,
3046
+ long newPositionMs ,
3047
+ long newContentPositionMs ,
2975
3048
int discontinuityReason ) {
2976
3049
PositionInfo newPositionInfo =
2977
3050
new PositionInfo (
@@ -2980,8 +3053,8 @@ private static PlayerInfo maskTimelineAndPositionInfo(
2980
3053
timeline .getWindow (newMediaItemIndex , new Window ()).mediaItem ,
2981
3054
/* periodUid= */ null ,
2982
3055
newPeriodIndex ,
2983
- playerInfo . sessionPositionInfo . positionInfo . positionMs ,
2984
- playerInfo . sessionPositionInfo . positionInfo . contentPositionMs ,
3056
+ newPositionMs ,
3057
+ newContentPositionMs ,
2985
3058
playerInfo .sessionPositionInfo .positionInfo .adGroupIndex ,
2986
3059
playerInfo .sessionPositionInfo .positionInfo .adIndexInAdGroup );
2987
3060
return maskTimelineAndPositionInfo (
0 commit comments