Playback of linear adverts
Layout XML to support linear ads
For each layout and screen density that needs to support ads, you may need to edit the layout files.
The following example shows a layout identified as ad_container
being allocated for the rendering of linear adverts.
This must be separate from the container of NMPVideoView since Google IMA overrides touch event handling when it renders adverts and when those complete the control bar must not be impacted.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/nmpVideoViewContainer"
>
<nagra.nmp.sdk.NMPVideoView
android:id="@+id/surfaceview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
...
</RelativeLayout>
<RelativeLayout
android:id="@+id/ad_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"/>
...
Update the player's Java source code.
Import the IMAWrapper classes (adjust the package path if you changed the source code).
JAVAimport com.nagra.example.dynamicima.adverts.IMAWrapper; import com.nagra.example.dynamicima.adverts.IMAWrapperDelegate;
In the code for the class that extends Activity, two additional attributes are required, one for IMAWrapper and one for the IMAWrapperDelegate.
Implement the wrapper delegate.
The delegate needs to implement functions for the following to handle the interactions between the adverts manager and the application.JAVAprivate IMAWrapperDelegate mIMADelegate = new IMAWrapperDelegate() { @Override public void resumeContent(IMAWrapperResumeType xResumeType) { // Warning: Consider whether it is appropriate to (re-)start here // It is advisable to check if the content has completed mVideoView.start(); } @Override public void pauseContent() { mVideoView.pause(); } @Override public long getContentPosition() { return mVideoView.getCurrentPosition(); } @Override public long getContentDuration() { if (null != mVideoView) { return mVideoView.getDuration(); } return 0; } @Override public void advertStarted() { ... } @Override public void completedCallback() { ... } @Override public void logEvent(Map<String, String> xAdData) { ... } ... }
To ensure that pre-roll adverts play before the content, you are advised to hold back the
NMPVideoView.start()
method until the pre-roll adverts have played, and not played as soon as it would have been if there were no adverts. So that the content does not re-start after post-roll adverts play, it is advised that a check is made to see if the content has already completed.
Between adverts in a pod, or just before the start of an advert, a freeze frame of the content will be seen unless action is taken to avoid it. If you prefer to display a black frame, remove the video view when adverts start and reinstate it on completion.
Any latency with the Google IMA system starting playback of an advert may elongate the freeze frame or black screen. To reduce the black frame time, the remove action can be taken as late as possible, such as on receipt of the Ad Started event. You can do this by removing the view when theIMAWrapperDelegate advertStarted()
method is called.
To log more information about the Google IMA system, you can implement appropriate code in thelogEvent()
method.
Google IMA reports errors via theIMAWrapper
and this method in theIMAWrapperDelegate
.Instantiate the wrapper.
During creation of this activity, such as within theonCreate()
method, you must create and configure theIMAWrapper
member and then callstartAdsServices()
on it, passing the user interfaceViewGroup
set aside for linear adverts.JAVAViewGroup adUiContainerViewGroup = (ViewGroup) findViewById(R.id.nmpVideoViewContainer); mIMAWrapper = new IMAWrapper(mContext); mIMAWrapper.startAdsServices(adUiContainerViewGroup);
The call to
startAdsServices()
is slightly different if companion ads are also used – see below.Associate the delegate to the wrapper.
The wrapper needs to be made aware of the delegate via the setDelegate() method:JAVAmIMAWrapper.setDelegate(mIMADelegate);
Alternatively, you can pass the delegate to the wrapper's contructor when you create it:
mIMAWrapper = new IMAWrapper(mContext, mIMADelegate);
Request the ads.
Pass the advert tag URI into theIMAWrapper
via therequestAds()
method:JAVAmIMAWrapper.requestAds(xAdvertTag);
You should call
requestAds()
as early as possible to minimise latency in retrieving the ads.
Replacing playback content
Destroying the IMAWrapper
instance cancels any scheduled ads and terminates a playing ad if it exists. If you keep the IMAWrapper
instance, you must request a new ad session with requestAds()
whenever the playback content changes. A stopIMAServices()
method is provided in the IMAWrapper
to clean up an old configuration and its associated instances.
Next step: Enable playback of companion adverts