Skip to content

Using LemmaSDK API

lmsdkdev edited this page Apr 15, 2019 · 9 revisions

Please Refer to Sample application for reference code.

Initialization of LemmaSDK library

LemmaSDK needs to have SD cards write permission for a better user experience; LemmaSDK pre-downloads the ads and plays them from local storage.

 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mLinerAdContainer = (FrameLayout) findViewById(R.id.ad_container);

    String listPermissionsNeeded[] = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_COARSE_LOCATION};

    if (ContextCompat.checkSelfPermission(this, listPermissionsNeeded[0]) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, listPermissionsNeeded[1]) == PackageManager.PERMISSION_GRANTED) {

         loadAd();

    } else {
        ActivityCompat.requestPermissions(this, listPermissionsNeeded, REQUEST_ID_MULTIPLE_PERMISSIONS);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    loadAd();
}

private void loadAd() {
    LemmaSDK.init(this);
    LMAdRequest adRequest = new LMAdRequest(<publisher_id_string>, <ad_unit_id_string>);

    mVAdManager = new LMVideoAdManager(Context, adRequest, new AdManagerCallback() {
        @Override
        public void onAdError(LMVideoAdManager adManager, Error error) {
            // Log Error
        }

        @Override
        public void onAdEvent(AD_EVENT event) {

            AppLog.i(TAG, event.name());
            AppLog.i(TAG, "" + mVAdManager.getCurrentAdLoopStat());

            switch (event) {
                case AD_LOADED:
                    mVAdManager.startAd();
                    break;
                default:
                    break;
            }
        }
    });
    mVAdManager.init(mLinerAdContainer);

}

In the above sample code,

  • Context : It can be an activity or fragment
  • adRequest : Valid LMAdRequest object having publisher_id_string and ad_unit_id_string.
  • AdManagerCallback : Class object which implements AdManagerCallback abstract class methods
  • publisher_id_string : Valid publisher id string shared by Lemma
  • ad_unit_id_string : Valid ad unit id id string shared by Lemma
  • ViewGroup : It can be FrameLayout or other layouts as per applications user interface and in which region you want to show the video ad.

Interact with LemmaSDK using AdManagerCallback listener methods

Error call back

public void onAdError(LMVideoAdManager adManager, Error error) {
   // Log Error
}

Above callback is invoked by SDK when the Ad manager fails to play or process the ad.

Ad event call back

@Override
public void onAdEvent(AD_EVENT event) {
    switch (event) {
        case AD_LOADED:
            mVAdManager.startAd();
            break;
        default:
            break;
    }
}

The above callback is invoked by SDK on various Ad events. Below table has detailed lists of Ad events. the Application code can implement the behavior required for each Ad Event; Some of these events are optional to implement;

Event Description
AD_LOADED Ad has Loaded and ready to be played
AD_STARTED Ad has started playing
AD_PAUSED when mVAdManager.pauseLoop() is called, this event is fired when current playing ad is completed
AD_RESUMED when mVAdManager.resumeLoop() is called, this event is fired immediately
AD_FIRST_QUARTILE Ad has completed 25% of its content
AD_MID_POINT Ad has completed 50% of its content
AD_THIRD_QUARTILE Ad has completed 75% of its content
AD_COMPLETED Ad has completed 100% of its content
AD_LOOP_COMPLETED Ad has completed whole ad loop

Get statistics of running ad loop

Following API methods can be used to get stats on running Ad loop

Method Description
mVAdManager.getCurrentAdLoopStat().getCurrentAdIndex() Returns index of current ad in ad loop
mVAdManager.getCurrentAdLoopStat().getCurrentAdLoopLength() Returns length of current ad loop
mVAdManager.getCurrentAdLoopStat().getCurrentAdCreativeName() Returns the current playing Creative Name
mVAdManager.getCurrentAdLoopStat().getCurrentAdDuration() Returns the Current ad duration in second
Example
Log.i(TAG, "" + mVAdManager.getCurrentAdLoopStat());

Pause/Resume ads loop

If you are playing lemma ads in between your video contents or your own ads. you can make use of these API to pause/resume lemma ads in between and schedule/play your content. ads can be paused/resumed multiple times using a single video manager instance. If you only want to show lemma ad one time in applications lifecycle. you can clean up lemma video manager after its use, by using Video manager cleanup steps.

You can pause ad when an ad completed

 if (mVAdManager.getCurrentAdLoopStat().getCurrentAdIndex() == 2 &&
            event == AD_COMPLETED){
        mVAdManager.pauseLoop();
 }

After ad is paused player view may be visible to end user with last video frame loaded in it. if you want to hide that you can do so by using 'mVAdManager.setPlayerVisibility(View.INVISIBLE);'. Also note that if you have made player invisible while pausing ad, you have to make is visible mVAdManager.setPlayerVisibility(View.VISIBLE); while resuming. SDK will not automatically make is visible on resume.

or the whole ad loop is completed

 if (event == AD_LOOP_COMPLETED){
        mVAdManager.pauseLoop();
 }

Ad loop that is in paused state can be resumed using resume()

mVAdManager.resumeLoop();

Video manager cleanup.

When mVAdManager.startAd(); called, SDK will start playing ads on ViewGroup region provided by host application. To stop ads playing call mVAdManager.destroy(); for cleanup. After this you should not call any method on mVAdManager. If you want to show ads from SDK again you have to create new 'LMVideoAdManager' instance.

Sample code

@Overvride
public void onAdEvent(AD_EVENT event) {
        mVAdManager.destroy();
        mVAdManager = null;
}

Technical Support

For any technical support in integration please contact [email protected]

Clone this wiki locally