Android Open Source - snakk-ads-android-sample-app T V A S T Tracking Video View






From Project

Back to project page snakk-ads-android-sample-app.

License

The source code is released under:

Copyright (c) 2012, Snakk! Media Group All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are m...

If you think the Android project snakk-ads-android-sample-app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.snakk.vastsdk.player;
/*from  w w  w  .ja  v  a2s . c o  m*/
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.widget.VideoView;
import com.snakk.vastsdk.player.TVASTPlayer.TVASTAdPlayerListener;

import java.util.ArrayList;
import java.util.List;

/**
 * A VideoView that intercepts various methods and reports them back to a set of
 * TVASTAdPlayerListener.
 */
public class TVASTTrackingVideoView extends VideoView implements OnCompletionListener, OnErrorListener {
    private static final String TAG = "Snakk";

    private enum PlaybackState {
        STOPPED, PAUSED, PLAYING
    }

    private final List<TVASTAdPlayerListener> callbacks = new ArrayList<TVASTAdPlayerListener>(1);
    private TVASTVideoProgressThread progressThread;
    private PlaybackState state = PlaybackState.STOPPED;
    private TVASTPlayer mPlayer;

    public TVASTTrackingVideoView(Context context) {
        super(context);
        init(null);
    }

    public TVASTTrackingVideoView(Context context, TVASTPlayer player) {
        super(context);
        init(player);
    }

    private void init(TVASTPlayer player) {
        super.setOnCompletionListener(this);
        super.setOnErrorListener(this);

        this.mPlayer = player;

        OnPreparedListener preparedListener = new OnPreparedListener() {
            @Override
            public void onPrepared(final MediaPlayer mp) {
                for (TVASTAdPlayerListener callback : callbacks) {
                    callback.onVideoPlay(mPlayer);
                }
            }
        };
        setOnPreparedListener(preparedListener);
    }

//    // called from DemoPlayer
//    public void onClick() {
//        SnakkLog.d(TAG, "TVASTTrackingVideoView.onClick");
//        stopPlayback();
//        for (TVASTAdPlayerListener callback : callbacks) {
//            callback.onVideoClick(mPlayer);
//        }
//    }
//
    // Overrides methods of VideoView
    @Override
    public void start() {
        super.start();
        PlaybackState oldState = state;
        state = PlaybackState.PLAYING;

        switch (oldState) {
            case STOPPED:
                progressThread = new TVASTVideoProgressThread(this.mPlayer, callbacks);
                progressThread.start();
                break;
            case PAUSED:
                for (TVASTAdPlayerListener callback : callbacks) {
                    callback.onVideoResume(mPlayer);
                }
                break;
            default:
                // Already playing; do nothing.
        }
    }

    @Override
    public void pause() {
        super.pause();
        state = PlaybackState.PAUSED;

        for (TVASTAdPlayerListener callback : callbacks) {
            callback.onVideoPause(mPlayer);
        }

        // pause state doesn't work well with streaming so call stopPlayback.
        stopPlayback();
    }

    @Override
    public void resume() {
        super.resume();

        for (TVASTAdPlayerListener callback : callbacks) {
            callback.onVideoResume(mPlayer);
        }
    }

    @Override
    public void stopPlayback() {
        super.stopPlayback();
        onStop(null);
    }

    private void onStop(MediaPlayer mp) {

        if (state == PlaybackState.STOPPED) {
            return; // Already stopped; do nothing.
        }

        state = PlaybackState.STOPPED;

        if (progressThread != null) {
            progressThread.quit();
            try {
                progressThread.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            progressThread = null;
        }
    }

    // Method of OnCompletionListener
    @Override
    public void onCompletion(MediaPlayer mp) {
        onStop(mp);
        //synchronized(callbacks)
        {
            for (TVASTAdPlayerListener callback : callbacks) {
                callback.onVideoComplete(mPlayer);
            }
        }
    }

    // Method of OnErrorListener
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        //synchronized(callbacks)
        {
            for (TVASTAdPlayerListener callback : callbacks) {
                callback.onVideoError(mPlayer);
            }
        }
        onStop(mp);

        // needs to call on Listeners about the error.
    /*
    what == MediaPlayer.MEDIA_ERROR_UNKNOWN
  what = MEDIA_ERROR_UNKNOWN;
  
  MEDIA_ERROR_IO, 
  MEDIA_ERROR_MALFORMED,
  MEDIA_ERROR_UNSUPPORTED,
  MEDIA_ERROR_TIMED_OUT
  */

        // Returning true signals to MediaPlayer that we handled the error. This will prevent the
        // completion handler from being called.
        return true;
    }

    public void addCallback(TVASTAdPlayerListener callback) {
        if (callbacks.contains(callback))
            return;
        synchronized (callbacks) {
            callbacks.add(callback);
        }
    }

    public void removeCallback(TVASTAdPlayerListener callback) {
        synchronized (callbacks) {
            callbacks.remove(callback);
        }
    }

    @Override
    public void setOnCompletionListener(OnCompletionListener l) {
        throw new UnsupportedOperationException();
    }
}




Java Source Code List

com.snakk.advertising.Config.java
com.snakk.advertising.SnakkAdPrompt.java
com.snakk.advertising.SnakkAdRequest.java
com.snakk.advertising.SnakkAdvertising.java
com.snakk.advertising.SnakkBannerAdView.java
com.snakk.advertising.SnakkInterstitialAd.java
com.snakk.advertising.SnakkVideoInterstitialAd.java
com.snakk.advertising.internal.AbstractStatefulAd.java
com.snakk.advertising.internal.AdActivityContentWrapper.java
com.snakk.advertising.internal.AdPromptImpl.java
com.snakk.advertising.internal.AdRequestImpl.java
com.snakk.advertising.internal.BasicWebView.java
com.snakk.advertising.internal.DeviceCapabilities.java
com.snakk.advertising.internal.InterstitialAdImpl.java
com.snakk.advertising.internal.InterstitialBaseView.java
com.snakk.advertising.internal.Sharable.java
com.snakk.advertising.internal.SnakkAdActivity.java
com.snakk.advertising.internal.VastPlayerView.java
com.snakk.advertising.internal.VideoInterstitialAdImpl.java
com.snakk.advertising.package-info.java
com.snakk.adview.AdFullscreenView.java
com.snakk.adview.AdInterstitialBaseView.java
com.snakk.adview.AdInterstitialView.java
com.snakk.adview.AdLog.java
com.snakk.adview.AdOfferWallView.java
com.snakk.adview.AdPrompt.java
com.snakk.adview.AdRequest.java
com.snakk.adview.AdVideoUnitView.java
com.snakk.adview.AdViewCore.java
com.snakk.adview.AdView.java
com.snakk.adview.AutoDetectedParametersSet.java
com.snakk.adview.Base64.java
com.snakk.adview.DeprecatedInterstitialBaseView.java
com.snakk.adview.IMraidCommand.java
com.snakk.adview.LockedOrientationDialog.java
com.snakk.adview.MraidCommand.java
com.snakk.adview.Mraid.java
com.snakk.adview.OldAdActivity.java
com.snakk.adview.Rotate3dAnimation.java
com.snakk.adview.Utils.java
com.snakk.adview.track.EventTracker.java
com.snakk.adview.track.InstallTracker.java
com.snakk.adview.package-info.java
com.snakk.core.SnakkLog.java
com.snakk.vastsdk.TVASTAdErrorEvent.java
com.snakk.vastsdk.TVASTAdErrorListener.java
com.snakk.vastsdk.TVASTAdError.java
com.snakk.vastsdk.TVASTAdType.java
com.snakk.vastsdk.TVASTAdView.java
com.snakk.vastsdk.TVASTAd.java
com.snakk.vastsdk.TVASTAdsLoader.java
com.snakk.vastsdk.TVASTAdsRequest.java
com.snakk.vastsdk.TVASTCompanionAdSlot.java
com.snakk.vastsdk.TVASTCompanionAd.java
com.snakk.vastsdk.TVASTCreative.java
com.snakk.vastsdk.TVASTLinearAd.java
com.snakk.vastsdk.TVASTLinearIcon.java
com.snakk.vastsdk.TVASTMediaFile.java
com.snakk.vastsdk.TVASTNonlinearAd.java
com.snakk.vastsdk.TVASTPostbackTask.java
com.snakk.vastsdk.TVASTUtils.java
com.snakk.vastsdk.TVASTVideoAdsManager.java
com.snakk.vastsdk.player.TVASTPlayer.java
com.snakk.vastsdk.player.TVASTSharable.java
com.snakk.vastsdk.player.TVASTTrackingVideoView.java
com.snakk.vastsdk.player.TVASTVideoProgressThread.java
com.snakkads.tabsswipe.BannerAdPromptFragment.java
com.snakkads.tabsswipe.BannerInterstitialFragment.java
com.snakkads.tabsswipe.BannerRichMediaFragment.java
com.snakkads.tabsswipe.BannerStandardMediaFragment.java
com.snakkads.tabsswipe.BannerVideoFragment.java
com.snakkads.tabsswipe.MainActivity.java
com.snakkads.tabsswipe.SplashScreen.java
com.snakkads.tabsswipe.adapter.TabsPagerAdapter.java
com.yourcompany.AdMobActivity.java
com.yourcompany.SampleActivity.java