How to play EPUB with Media Overlays with background Audio support
Introduction
The ColibrioReadingSystem allows you to load audiobooks in the EPUB with Media Overlays format and play them in the background. In this guide we explain how a project is set up to use this feature, and how to use it.
How to configure the project
Please refer to this article Playing an Audiobook for instructions on how to configure your project to use this feature.
How to load the EPUB with MediaOverlays
Loading the EPUB file is done using the ReadingSystemEngine:
Create a
ZipResourceProviderby callingZipResourceProvider.create(dataSource: RandomAccessDataSource, zipResourceProviderOptions: ZipResourceProviderOptions).Load the EPUB publication into the engine by calling
ReadingSystemEngine.loadEpub(config: EpubResourceProviderLoadConfig). In theEpubResourceProviderLoadConfig, pass yourZipResourceProvider. You will get an instance ofEpubReaderPublication.Prepare the
ReaderView(which you access viaColibrioReadingSystemEngine.readerView) for rendering the publication. To achieve this you add a renderer on theReaderViewand set the readerDocuments toEpubReaderPublication.spine.Create a
SyncMediaTimelineby callingcreateMediaOverlaySyncMediaTimeline(readerDocuments: List<ReaderDocument>, config: MediaOverlaySyncMediaTimelineConfiguration, progressCallback: (Double) -> Unit)on theEpubReaderPublication.Get an
EpubAudioPublicationby callingAudioReadingSystemEngine.loadEpubMediaOverlayAudioPublication(config: EpubMediaOverlayLoadConfig). In theEpubMediaOverlayLoadConfigyou pass theEpubReaderPublicationand theSyncMediaTimeline.Create a
ColibrioAudioPlayerusingAudioReadingSystemEngine.createAudioPlayer(colibrioAudioPlayerConfig: ColibrioAudioPlayerConfig). You specify aColibrioAudioPlayerConfigsupplying theAudioTimelinewhich you get fromEpubAudioPublication.timeline,ColibrioMediaSessionMetadataandColibrioMediaCommands.Add event listeners to the player using
ColibrioAudioPlayer.addOnMediaPlayerEventListener(listener: OnMediaPlayerEventListener)to get notified about playback state and errors. You can remove your listeners usingColibrioAudioPlayer.removeOnMediaPlayerEventListener(listener: OnMediaPlayerEventListener).Use the functions on the
ColibrioAudioPlayerto control playback such asplay(),pause()and the seek functions.
How to sync native Audio and The Readerview with a SyncMediaPlayer
After you have created the ColibrioAudioPlayer, follow these steps to synchronize the ReaderView to the ColibrioAudioPlayer.
Create a
SyncMediaPlayerby callingReadingSystemEngine.createSyncMediaPlayer(colibrioAudioPlayer: ColibrioAudioPlayer)using theColibrioAudioPlayer.Assign the created
SyncMediaPlayerto theReaderViewand synchronize against theSyncMediaPlayer's current position by callingReaderView.setSyncMediaPlayer(syncMediaPlayer: SyncMediaPlayer?, initialSyncMethod: SyncMediaReaderViewSynchronizationMethod)using theSyncMediaPlayerand the “reader view goto”SyncMediaReaderViewSynchronizationMethod.
Supporting audio playback when the ReaderView is no longer active
If you want audio playback to resume in the background when the user puts the app in the background or when they leave the current view and the ReaderView is destroyed, then you need to detach the ReaderView from your ColibrioAudioPlayer.
Just before the
ReaderViewis destroyed, you can detach theReaderViewfrom yourColibrioAudioPlayersimply by destroying theSyncMediaPlayerwhich you do by callingReadingSystemEngine.destroySyncMediaPlayer(player: SyncMediaPlayer). You can then continue using theColibrioAudioPlayerfor background playback.When the ReaderView is created and you want to achieve synchronization again, follow the previous steps regarding loading EPUB with MediaOverlays publications. In this case you do not create a new
ColibrioAudioPlayer, but move on to creating aSyncMediaPlayerusing the existingColibrioAudioPlayerand assigning it to theReaderViewas explained in the synchronization section above.
How to configure the media session
Please refer to this article Playing an Audiobook for instructions.
How to control playback
Please refer to this article Playing an Audiobook for instructions.
Handling playback errors
Please refer to this article Playing an Audiobook for instructions.