A Closer Look at the Android Sample App

A closer look at the source code of the sample app highlights the configuration points you need focus on to build your own app.

Setup the Library for Your Project

For a new project, be sure to put the following jar files in the lib or libs directory:

  • OoyalaSDK.jar: The jar file from the baseline Ooyala Mobile SDK for Android.
  • OoyalaIMASDK.jar: The jar file from the Ooyala SDK for Google IMA on Android.

Update the AndroidManifest.xml File

In the AndroidManifest.xml file, add the android:configChanges attribute (see highlighted line below) on the applications' <activity> declaration.

Note: This is only required if you use the OptimizedOoyalaPlayerLayoutController, which is recommended. This is discussed further below.

<application>
  <activity
    android:name="com.ooyala.android.imasampleapp.IMASampleAppActivity"
    android:label="@string/app_name"
    android:configChanges="orientation|keyboardHidden" >
...
  </activity>
</application>

The Source for the Sample App

Open the IMASampleApp/src/ooyala/com/android/imasampleapp/IMASampleAppActivity.java file.

The Imports

Examine the import statements at the top of the file:
  • The first two import statements pull in definitions from the Ooyala SDK for Google IMA.
  • The next four are standard Android imports.
  • The final three pull definitions for players and view controllers from the baseline Ooyala Mobile SDK for Android.
import com.ooyala.android.imasdk.*;
import com.ooyala.android.imasampleapp.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import com.ooyala.android.OoyalaPlayer;
import com.ooyala.android.OoyalaPlayerLayout;
import com.ooyala.android.OptimizedOoyalaPlayerLayoutController; 
...

The Constants

The topic Creating a Basic Android App introduces the constants PCODE, EMBED, and new PlayerDomain(DOMAIN):
  final String EMBED  = "h5OWFoYTrG4YIPdrDKrIz5-VhobsuT-M";  //Embed Code, or Content ID
  final String PCODE  = "R2d3I6s06RyB712DN0_2GsQS-R-Y";
  final String new PlayerDomain(DOMAIN) = "http://www.ooyala.com";
...
Whereas the sample app defines these as constants, you probably want to define variables, especially for the EMBED constant (asset ID or content ID).

Set Up the Controller, Initialize the OoyalaIMAManager and Classes

Various layout controllers combined with the OoyalaIMAManager are included with the SDK. The basic views you can choose from are included in the baseline Mobile SDK for Android.

Note: For working with Google IMA, it is highly recommended that you use the OptimizedOoyalaPlayerLayoutController layout controller.

In the sample app, a layout controller is declared of type OptimizedOoyalaPlayerLayoutController.

Finally, the imaManager object is initialized as type OOIMAManager with a player:

  OptimizedOoyalaPlayerLayoutController playerLayoutController;
  OoyalaIMAManager imaManager;
/**
   * Called when the activity is first created.
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    OoyalaPlayerLayout playerLayout = (OoyalaPlayerLayout) findViewById(R.id.ooyalaPlayer);
    playerLayoutController = new OptimizedOoyalaPlayerLayoutController(playerLayout, PCODE, new PlayerDomain(DOMAIN));
    OoyalaPlayer player = playerLayoutController.getPlayer();

    //Initialize IMA classes
    imaManager = new OoyalaIMAManager(player);
Note: For reference documentation about the OoyalaIMAManager, see the Ooyala IMA SDK-Android API Documentation.

Companion Ad Slot

A companion view is declared of type ViewGroup, and companion ad slot with that view, with width and height in pixels, is defined for the imaManager object.

    ViewGroup companionView = (ViewGroup) findViewById(R.id.companionFrame);
    imaManager.addCompanionSlot(companionView, 300, 50);

Append Ad Tag Parameters to your Ad Tag URL

To append your own ad tags to your Google-supplied ad tag URL, use the setAdTagParameters() method.

  1. First, create a HashMap with two strings to hold the parameters you want to add to the ad tag URL. In the sample app, the hash is called adTagParameters.
      Map<String, String> adTagParameters = new HashMap<String, String>();
      adTagParameters.put("vid", EMBED);
      adTagParameters.put("url", "[referrer_url]");
      adTagParameters.put("pod", "2");
      adTagParameters.put("ppos", "2");
      adTagParameters.put("vpos", "preroll");
      adTagParameters.put("mridx", "2");
      imaManager.setAdTagParameters(adTagParameters);
  2. For each parameter you want to add, use put() to place the tag name/value pair into the hash. In the sample, notice that the first ad tag name/value pair sets the vid tag to the value of the EMBED constant (the video or asset ID).
  3. Finally, after you have populated the hash, pass it to the imaManager.setadTagParameters() method.

    As an example, suppose you have an adTagUrl that looks like the following (this URL is broken across several lines for readability:

    http://pubads.g.doubleclick.net/gampad/ads?sz=400x300&iu=%2F6062%2Fhanna_MA_group%2Fwrapper_with_comp
    &ciu_szs=728x90&impl=s&gdfp_req=1&env=vp&output=xml_vast2&unviewed_position_start=1
    &m_ast=vast&correlator=[timestamp]
    After setting up your ad tag parameters as shown in the sample app, the ad tag URL would look like this, with the extra tags highlighted:
    http://pubads.g.doubleclick.net/gampad/ads?sz=400x300&iu=%2F6062%2Fhanna_MA_group%2Fwrapper_with_comp
    &ciu_szs=728x90&impl=s&gdfp_req=1&env=vp&output=xml_vast2&unviewed_position_start=1
    &m_ast=vast&correlator=[timestamp]
    &mridx=2&vpos=preroll&ppos=2&vid=h5OWFoYTrG4YIPdrDKrIz5-VhobsuT-M&pod=2&url=[referrer_url]
You can also override the base value of the ad tag URL itself. See the discussion in Overriding the Ad Tag URL on Android.

Play the Video

Finally, the video is played with the setEmbedCode() method, which comes with the baseline Mobile SDK:

  if (player.setEmbedCode(EMBED)) {
    player.play();
  } else {
    Log.d(this.getClass().getName(), "Something Went Wrong!");
   

Was this article helpful?