Adding In-stream Ads to your Android app

Video publishers can now use Audience Network's In-stream solution to deliver video ads to their global audience across mobile and desktop environments in pre-roll and mid-roll settings. Follow this guide to display this type of ad unit for Android-based video content. For monetizing your web video content with in-stream ads, see the Web implementation guide. For in-stream video ads within iOS mobile apps, see the iOS implementation guide. Or see a list of available types.

Requirements

Please refer to the "In-Stream Video Requirements" section of the Design Guidelines for Audience Network Ads.

Implementation

This guide will walk you through implementing in-stream video ads on Android following these steps:

Step 1: Apply for Audience Network In-stream Video

Step 2: Create Placements

Step 3: Load and Show In-stream Video Ad



Ensure you have completed the Audience Network Getting Started and Android Getting Started guides before you proceed.

Step 1: Apply for Audience Network In-stream Video

Go to your app dashboard and from the Audience Network menu on the left side of the screen, select Apps and Websites. Then, go to the "Apply for in-stream video ads" section. Please review the Requirements and, if applicable, click on the Apply button and follow the on screen instructions.

Step 2: Create Placements

Audience Network offers 4 types of ad units: banner, interstitial (app only), native ads, and In-stream Video. You will see In-stream Video as a Display Format type after you have been approved for In-stream Video. Each ad unit in your app or website is identified using a unique placement ID. The first step in adding an ad unit is to create this identifier.

Click the Create Ad Placement button to start creating placement IDs to test in your video player.

In the Create Ad Placement form, enter a Name for your placement, information on how to invoke the ad in the Steps to Trigger Ad, and select In-stream Video as the Display Format.

Create a separate placement for each location in your website or app where you want to show ads. This will allow better insights tracking and performance calibration.

If you are using Audience Network In-stream Video for multiple properties such as desktop, mobile web, and mobile apps, be sure to name your placements accordingly (e.g. desktop_placement_1 and iOS_placement_1) as this will allow you to differentiate your placements and performance across platforms.

Once you click Save, you will see your new placement listed on the Placements tab.

To learn more about creating placements, please refer to our FAQ. To maximize your revenue, check out best practices for the Performance Optimization tool.

Step 3: Load and Show In-stream Video Ad

Ensure you have completed the Audience Network Getting Started and Android Getting Started guides before you proceed.

Add the following code at the top of your activity class in order to import the Facebook Ads SDK:

import com.facebook.ads.*;

Next, instantiate an InstreamVideoAdView object and make a request to load an ad. This can also be done ahead of time, to pre-cache an ad response. In place of YOUR_PLACEMENT_ID, use the id of the in-stream video placement you created earlier.

When there is no ad to show, the onError will be called with error.code set to 1001. You should detect this and return to the content.

public class MainActivity extends AppCompatActivity {
  private InstreamVideoAdView adView;

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

    loadInstreamAd();
  }

  private int pxToDP(int px) {
    return (int)(px / this.getResources().getDisplayMetrics().density);
  }

  private void loadInstreamAd(){
    // Get the Ad Container
    LinearLayout adContainer = (LinearLayout) findViewById(R.id.banner_container);
    adView = new InstreamVideoAdView(
      this,
      YOUR_PLACEMENT_ID,
      new AdSize(
        pxToDP(adViewContainer.getMeasuredWidth()),
        pxToDP(adViewContainer.getMeasuredHeight())
      )
    );
    // set ad listener to handle events
    adView.setAdListener(new InstreamVideoAdListener() {
      @Override
      public void onAdLoaded(Ad ad) {
        // we have an ad so let's show it
        adContainer.addView(adView);
        adView.show();
      }


      @Override
      public void onAdVideoComplete(Ad ad) {

      }

      @Override
      public void onError(Ad ad, AdError adError) {

      }

      @Override
      public void onAdClicked(Ad ad) {

      }
    });
    adView.loadAd();
}

Finally, add the following code to your activity's onDestroy() function to release resources the adView use:

@Override
protected void onDestroy() {
  if (adView != null) {
    adView.destroy();
  }
  super.onDestroy();
}

When running on the simulator, test ads will be shown by default. To enable test ads on a device, add the following line of code before loading an ad: AdSettings.addTestDevice("HASHED ID");. Use the hashed ID that is printed to the log cat when you first make a request to load an ad on a device.

Next Steps