THEOplayer / react-native-theoplayer

A React Native THEOplayerView component
https://theoplayer.github.io/react-native-theoplayer/
MIT License
58 stars 21 forks source link

Setting `currentTime` during an `AD_BREAK_END` event does not work #223

Closed webnard closed 10 months ago

webnard commented 10 months ago

I am testing IMA ads on iOS and trying to implement a workaround for https://github.com/THEOplayer/react-native-theoplayer/issues/207. The player ignores setting the currentTime value during an AD_BREAK_END AD_EVENT callback.

  const onPlayerReady = async (p: THEOplayer) => {
    p.addEventListener(PlayerEventType.AD_EVENT, function (event: AdEvent) {
      if (event.subType === AdEventType.AD_BREAK_END) {
        p.currentTime = 35000 // is ignored
      }
    });
wvanhaevre commented 10 months ago

Setting the currentTime on the AdEvent indeed 'is ignored' as the player is still in the context of playing the ad. You are receiving the adBreakEnd event at the time the ad has finished playing, but the player has not yet switched back to the main content. Setting the currentTime at this time is still trying to change the timeValue for the ad.

I quickly did a test on native iOS and noticed that you could for example use the CAN_PLAY event. And could combine this with a boolean that indicates an AdBreak just ended.

For example: (swift code from iOS test)

self.canPlayListener = player.addEventListener(type: PlayerEventTypes.CAN_PLAY) { event in
            if self.adEnded {
                player.currentTime = 60
                self.adEnded = false
            }
}

where you set self.adEnded on the AdBreakEnd event:

self.adBreakEndListener = player.ads.addEventListener(type: AdsEventTypes.AD_BREAK_END) { event in
            self.adEnded = true
}

On RN the same flow should do the trick

webnard commented 10 months ago

Thanks, @wvanhaevre. This works. :)