This short how-to guide explains how to determine programmatically when the player is playing and whether it is playing a stream or a client-side advertisement. This may be useful if you need to determine different behavior (or UI) relating to the 2 mentioned options.
The question above is related to the following questions:
playing
event is triggered for an advertisement, or for the main content/stream?Web SDK | Android SDK | iOS SDK | tvOS SDK | Android TV SDK | Chromecast SDK |
---|---|---|---|---|---|
Yes | Yes | Yes | Yes | Yes | N/A |
There are certainly different ways to do so: it is enough to have a look at your player; or at the Network tab of your dev tools; or even directly at the manifest. Usually, the UI also has telltale signs. But there are many cases in which you may want to determine this programmatically, for example if you are to apply a different UI to your player depending on this factor.
You can use the playing
event to know when content (or an ad) starts playing. Its event handler is the correct scope to check whether an advertisement is playing through the player.ads.playing
property.
Let's see some code examples for the various SDKs.
function playingEventHandler(event) {
var adIsPlaying = player.ads.playing
console.log("PLAYING", adIsPlaying ? "Advertisement" : "Content", event)
}
player.addEventListener("playing", playingEventHandler)
final EventListener<PlayingEvent> playingEventHandler = new EventListener<PlayingEvent>() {
@Override
public void handleEvent(PlayingEvent playingEvent) {
boolean adIsPlaying = tpv.getPlayer().getAds().isPlaying();
System.out.println("PLAYING " + (adIsPlaying ? "Advertisement" : "Content"));
}
};
tpv.getPlayer().addEventListener(PlayerEventTypes.PLAYING, playingEventHandler);
self.eventListener = self.theoplayer.addEventListener(type: PlayerEventTypes.PLAYING) { [weak self] event in
self.theoplayer?.ads.requestPlaying() { (result, _) in
print("player.ads.playing = ", result!)
}
}
playing
event.