Skip to main content

Ad bookmarks

This guide shows how to implement bookmarking when using the ad insertion for video-on-demand (VOD) streams. This assumes a working ad insertion implementation, such as the one presented in Setup ad insertion.

Store a bookmark position

When playing a VOD with ads, number and position could change over time. It is necessary to take ads into account when storing a position for a later playback. The lib is providing a create a bookmark position API that translate the current position with ads to a position without ads and this value can be saved into the app.

public class PlaybackActivity extends Activity {
...

private StreamingSession mSession;

protected void onPause() {
// Generate the bookmark position with SmartLib
long bookmarkPosition = mSession.getPositionForBookmark();

// Store the position in your custom app storage
mAppStorage.storePosition(mContentId, bookmarkPosition);
}

...
}

Restore a bookmarked position

Load the bookmark when re-requesting a stream. To ensure that the restore a bookmark position API will return the correct result, call it once onAdData has been triggered (see Listen to ad data).

public class PlaybackActivity extends Activity {

// Your player object
private Object mPlayer;

// SmartLib StreamingSession
private StreamingSession mSession;

// It is recommended to use a different thread to start a session
private ExecutorService mExecutor = Executors.newSingleThreadExecutor();

...

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);

// Create the player
mPlayer = PlayerFactory.newInstance();

// Registering to player error events
mPlayer.setOnErrorEvent(this);

// Create a StreamingSession object
mSession = SmartLib.getInstance().createStreamingSession();

// Attach the player to the session
mSession.attachPlayer(mPlayer);

// Listen to ad data, triggered during SmartLib getURL
mSession.setAdDataListener(adList -> {
// Get the position from your custom app storage
long bookmarkPosition = mAppStorage.getPosition(mContentId);

// Generate the playback position with SmartLib
long playbackPosition = mSession.getPositionForPlayback(bookmarkPosition, false);
mPlayer.seek(playbackPosition);
mPlayer.play();
});

// Activate the advertising workflow
mSession.activateAdvertising();

// Start the session in a different thread, getURL can make requests to the Broadpeak CDN, nanoCDN...
mExecutor.submit(() -> {
StreamingSessionResult result = mSession.getURL(...);

if (!result.isError()) {
// Load URL without playing
mPlayer.loadURL(result.getURL());
} else {
mSession.stopStreamingSession();

showNoSessionCreatedMessage();
}
});
}

@Override
public void onPlayerError(PlaybackException error) {
// If the playback has been canceled because of a player error
// (i.e. non-recoverable error), stop the streaming session
if (error.playbackCanceled) {
mSession.stopStreamingSession();

showPlayerTriggeredAnErrorMessage();
}
}

...
}