6. Video Ad‎ > ‎

1) Android

Before starting this chapter, you have to got finished SDK settings. If not, check 3) Android Settings first.

For video Ad, first you have to load a video Ad by invoking TnkSession.prepareVideoAd() method and TnkSession.showVdieoAd() to play it.

[Loading a Video Ad]

Once your app has started, you have to call initialization method as belows.

Initialize

@Override

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // Initialized Tnkad Instance

        TnkSession.initInstance(this);



}


To load a video Ad, you have to invoke prepareVideoAd() method. Second parameter is the name of Display Logic. (See 5) Display Logic for detail) and third parameter is an instance of VideoAdListener which receives events of video loadings. Fourth parameter is repeat option. If you set this parameter to false, prepareVideoAd() method loads only one video Ad. After playing the video ad, you have to invoke prepareVideAd() again to load next one If you set the parameter to true. prepareVideoAd() automatically loads next video Ad after previous one has been played.

Prepare a Video Ad

        TnkSession.prepareVideoAd(this, "your_display_logic", new VideoAdListener() {

        // omit...

        }, true);


[VideoAdListener]

To get events from video Ads, you can put an instance of VideoAdListener in the third parameter of prepareVideAd() method.

VideoAdListener Interface

public interface VideoAdListener {


// User closed the video Ad by clicking close button or pressing back-key

public static final int CLOSE_SIMPLE = 0;


// User clicked the download button after watching video.

public static final int CLOSE_CLICK = 1;

public static final int FAIL_NO_AD = -1;  // no ad available available

public static final int FAIL_CANCELED = -4; // ad frequency settings 

public static final int FAIL_NOT_PREPARED = -5; // prepare not invoked.


public static final int FAIL_SYSTEM = -9;


/**

* Invoked when video Ad view is closed.

 * Reason for closing view is passed in type parameter

* @param type 

*/

public void onClose(int type);

/**

 * Invoked when video ad is shown up.

 */

public void onShow();

/**

* Invoked when failed to load a video ad.

 * If you set the repeat parameter to true in  prepareVideoAd(), this event is never invoked.

         */

public void onFailure(int errCode);

/**

* Invoked when a video ad has successfully loaded.

*/

public void onLoad();


       /**

 * Invoked when user completed watching the video ad.

         * If user click skip button while watching video, true is passed in parameter. 

 */

public void onVideoCompleted(boolean skipped);

}


[Video Ad Sample]

In this sample , a 'Play' button is shown when a video ad is loaded and the video is played when user clicks the button.Because a thread invoking VideoAdListener is not UI thread, Handler is used to control the visibility of 'Play' button.

Video Ad Sample

@Override

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // Initialized Tnkad Instance

        TnkSession.initInstance(this);

        TnkSession.setUserName(this, "user identification string");

        videoButton = (Button)findViewById(R.id.video_button);

        videoButton.setOnClickListener(new OnClickListener() {

                @Override

                public void onClick(View v) {

                        videoButton.setVisibility(View.GONE);

                        TnkSession.showVideoAd(MainActivity.this"intro_video");

                }

        });


        if (TnkSession.hasVideoAd(this, "intro_video")) {

                videoButton.setVisibility(View.VISIBLE);

        }

        else {

                videoButton.setVisibility(View.GONE);

                requestVideoAd();

        }

}


private Handler handler = new Handler();

private Runnable videoButtonShowRunnable = new Runnable() {

        @Override

        public void run() {

                if (TnkSession.hasVideoAd(MainActivity.this, "intro_video")) {

                        videoButton.setVisibility(View.VISIBLE);

                }

        }

};


private void requestVideoAd() {

        TnkSession.prepareVideoAd(this, "intro_video", new VideoAdListener() {

                @Override

                public void onClose(int type) {

                        Logger.d("video onClose : " + type);

                }

                @Override

                public void onShow() {

                        Logger.d("video onShow");

                }

                @Override

                public void onFailure(int errCode) {

                        // onFailure() never invoked when repeat parameter is true.

                        Logger.d("video onFailure");

                }

                @Override

                public void onLoad() {

                        Logger.d("video onLoad");

                        handler.post(videoButtonShowRunnable);

                }

                @Override

                public void onVideoCompleted(boolean skipped) {

                        Logger.d("video onVideoCompleted");

                }

        }, 

        true); // set repeat paramter to true

}



[Video Ad APIs]

- TnkSession.prepareVideoAd()

Method
  - void TnkSession.prepareVideoAd(Activity activity, VideoAdListener listener, boolean repeat)
  - void TnkSession.prepareVideoAd(Activity activity, String displayLogic, VideoAdListener listener, boolean repeat)

Description
   Load a video Ad. Once the video ad is loaded, you can play it by invoking showVideoAd() method.

Parameters
 Name Description
 activity Activity object
 displayLogic Name of Display Logic. See 5) Display Logic for detail.
 listener VideoAdListener Object
 repeat If set false, prepareVideoAd() method loads only one video Ad. After playing the video ad, you have to invoke prepareVideAd() again to load next one If set true. prepareVideoAd() automatically loads next video Ad after previous one has been played. 

- TnkSession.showVideoAd()

Method
  - void TnkSession.showVideoAd(Activity activity)
  - void TnkSession.showVideoAd(Activity activity, String displayLogic)

Description
   Play a video ad loaded by prepareVideoAd() method.

Parameters
 Name Description
 activity Activity object
 displayLogic Name of Display Logic. Set null if you want to play any video Ad regardless of Display Logic.

- TnkSession.hasVideoAd()

Method
  - boolean TnkSession.hasVideoAd(Context context)
  - boolean TnkSession.hasVideoAd(Context context, String displayLogic)

Description
  Returns true if a video ad is loaded and ready for playing.

Parameters
 파라메터 명칭 내용
 context Context object
 displayLogic Name of Display logic. Set null if you want to check any video ad regardless of Display Logic.
Comments