Java Code Examples for com.google.android.media.tv.companionlibrary.model.Advertisement#getStartTimeUtcMillis()

The following examples show how to use com.google.android.media.tv.companionlibrary.model.Advertisement#getStartTimeUtcMillis() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseTvInputService.java    From xipl with Apache License 2.0 6 votes vote down vote up
private void calculateElapsedTimesFromCurrentTime() {
    long currentTimeMs = getCurrentTime();
    mElapsedAdsTime = 0;
    mElapsedProgramTime = currentTimeMs - mCurrentProgram.getStartTimeUtcMillis();
    if (mCurrentProgram.getInternalProviderData() != null) {
        List<Advertisement> ads = mCurrentProgram.getInternalProviderData().getAds();
        for (Advertisement ad : ads) {
            if (ad.getStopTimeUtcMillis() < (currentTimeMs + PAST_AD_BUFFER_MILLIS)) {
                // Subtract past ad playback time to seek to
                // the correct content playback position.
                long adDuration = ad.getStopTimeUtcMillis() - ad.getStartTimeUtcMillis();
                mElapsedAdsTime += adDuration;
                mElapsedProgramTime -= adDuration;
            }
        }
    } else {
        Log.w(
                TAG,
                "Failed to get program provider data for "
                        + mCurrentProgram.getTitle()
                        + ". Try to do an EPG sync.");
    }
}
 
Example 2
Source File: BaseTvInputService.java    From androidtv-sample-inputs with Apache License 2.0 6 votes vote down vote up
private void calculateElapsedTimesFromCurrentTime() {
    long currentTimeMs = getCurrentTime();
    mElapsedAdsTime = 0;
    mElapsedProgramTime = currentTimeMs - mCurrentProgram.getStartTimeUtcMillis();
    if (mCurrentProgram.getInternalProviderData() != null) {
        List<Advertisement> ads = mCurrentProgram.getInternalProviderData().getAds();
        for (Advertisement ad : ads) {
            if (ad.getStopTimeUtcMillis() < (currentTimeMs + PAST_AD_BUFFER_MILLIS)) {
                // Subtract past ad playback time to seek to
                // the correct content playback position.
                long adDuration = ad.getStopTimeUtcMillis() - ad.getStartTimeUtcMillis();
                mElapsedAdsTime += adDuration;
                mElapsedProgramTime -= adDuration;
            }
        }
    } else {
        Log.w(
                TAG,
                "Failed to get program provider data for "
                        + mCurrentProgram.getTitle()
                        + ". Try to do an EPG sync.");
    }
}
 
Example 3
Source File: BaseTvInputService.java    From xipl with Apache License 2.0 5 votes vote down vote up
private boolean scheduleNextAd() {
    mHandler.removeMessages(MSG_PLAY_AD);
    if (mPlayingRecordedProgram) {
        return false;
    }
    long currentTimeMs = getCurrentTime();
    if (mCurrentProgram.getInternalProviderData() != null) {
        List<Advertisement> ads = mCurrentProgram.getInternalProviderData().getAds();
        Advertisement adToPlay = null;
        long timeTilAdToPlay = 0;
        for (Advertisement ad : ads) {
            if (ad.getStopTimeUtcMillis() > currentTimeMs + PAST_AD_BUFFER_MILLIS) {
                long timeTilAd = ad.getStartTimeUtcMillis() - currentTimeMs;
                if (timeTilAd < 0) {
                    // If tuning to the middle of a scheduled ad, the played portion
                    // of the ad will be skipped by the AdControllerCallback.
                    mHandler.sendMessage(mHandler.obtainMessage(MSG_PLAY_AD, ad));
                    return false;
                } else if (adToPlay == null || timeTilAd < timeTilAdToPlay) {
                    adToPlay = ad;
                    timeTilAdToPlay = timeTilAd;
                }
            }
        }

        if (adToPlay != null) {
            Message pauseContentPlayAdMsg = mHandler.obtainMessage(MSG_PLAY_AD, adToPlay);
            mHandler.sendMessageDelayed(pauseContentPlayAdMsg, timeTilAdToPlay);
        }
    } else {
        Log.w(
                TAG,
                "Failed to get program provider data for "
                        + mCurrentProgram.getTitle()
                        + ". Try to do an EPG sync.");
    }
    return true;
}
 
Example 4
Source File: BaseTvInputService.java    From xipl with Apache License 2.0 5 votes vote down vote up
private boolean insertAd(Advertisement ad) {
    if (DEBUG) {
        Log.d(TAG, "Insert an ad");
    }

    // If timeshifting, do not play the ad.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        long timeShiftedDifference =
                System.currentTimeMillis() - mTimeShiftedPlaybackPosition;
        if (mTimeShiftedPlaybackPosition != TvInputManager.TIME_SHIFT_INVALID_TIME
                && timeShiftedDifference > TIME_SHIFTED_MINIMUM_DIFFERENCE_MILLIS) {
            mElapsedAdsTime += ad.getStopTimeUtcMillis() - ad.getStartTimeUtcMillis();
            mTimeShiftedPlaybackPosition =
                    mElapsedProgramTime
                            + mElapsedAdsTime
                            + mCurrentProgram.getStartTimeUtcMillis();
            scheduleNextAd();
            scheduleNextProgram();

            // If timeshifting, but skipping the ad would actually put us ahead of
            // live streaming, then readjust to the live stream position.
            if (mTimeShiftedPlaybackPosition > System.currentTimeMillis()) {
                mTimeShiftedPlaybackPosition = TvInputManager.TIME_SHIFT_INVALID_TIME;
                playCurrentContent();
            }
            return false;
        }
    }

    releaseAdController();
    mAdController = new AdController(mContext);
    mAdController.requestAds(ad.getRequestUrl(), new AdControllerCallbackImpl(ad));
    return true;
}
 
Example 5
Source File: BaseTvInputService.java    From androidtv-sample-inputs with Apache License 2.0 5 votes vote down vote up
private boolean scheduleNextAd() {
    mHandler.removeMessages(MSG_PLAY_AD);
    if (mPlayingRecordedProgram) {
        return false;
    }
    long currentTimeMs = getCurrentTime();
    if (mCurrentProgram.getInternalProviderData() != null) {
        List<Advertisement> ads = mCurrentProgram.getInternalProviderData().getAds();
        Advertisement adToPlay = null;
        long timeTilAdToPlay = 0;
        for (Advertisement ad : ads) {
            if (ad.getStopTimeUtcMillis() > currentTimeMs + PAST_AD_BUFFER_MILLIS) {
                long timeTilAd = ad.getStartTimeUtcMillis() - currentTimeMs;
                if (timeTilAd < 0) {
                    // If tuning to the middle of a scheduled ad, the played portion
                    // of the ad will be skipped by the AdControllerCallback.
                    mHandler.sendMessage(mHandler.obtainMessage(MSG_PLAY_AD, ad));
                    return false;
                } else if (adToPlay == null || timeTilAd < timeTilAdToPlay) {
                    adToPlay = ad;
                    timeTilAdToPlay = timeTilAd;
                }
            }
        }

        if (adToPlay != null) {
            Message pauseContentPlayAdMsg = mHandler.obtainMessage(MSG_PLAY_AD, adToPlay);
            mHandler.sendMessageDelayed(pauseContentPlayAdMsg, timeTilAdToPlay);
        }
    } else {
        Log.w(
                TAG,
                "Failed to get program provider data for "
                        + mCurrentProgram.getTitle()
                        + ". Try to do an EPG sync.");
    }
    return true;
}
 
Example 6
Source File: BaseTvInputService.java    From androidtv-sample-inputs with Apache License 2.0 5 votes vote down vote up
private boolean insertAd(Advertisement ad) {
    if (DEBUG) {
        Log.d(TAG, "Insert an ad");
    }

    // If timeshifting, do not play the ad.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        long timeShiftedDifference =
                System.currentTimeMillis() - mTimeShiftedPlaybackPosition;
        if (mTimeShiftedPlaybackPosition != TvInputManager.TIME_SHIFT_INVALID_TIME
                && timeShiftedDifference > TIME_SHIFTED_MINIMUM_DIFFERENCE_MILLIS) {
            mElapsedAdsTime += ad.getStopTimeUtcMillis() - ad.getStartTimeUtcMillis();
            mTimeShiftedPlaybackPosition =
                    mElapsedProgramTime
                            + mElapsedAdsTime
                            + mCurrentProgram.getStartTimeUtcMillis();
            scheduleNextAd();
            scheduleNextProgram();

            // If timeshifting, but skipping the ad would actually put us ahead of
            // live streaming, then readjust to the live stream position.
            if (mTimeShiftedPlaybackPosition > System.currentTimeMillis()) {
                mTimeShiftedPlaybackPosition = TvInputManager.TIME_SHIFT_INVALID_TIME;
                playCurrentContent();
            }
            return false;
        }
    }

    releaseAdController();
    mAdController = new AdController(mContext);
    mAdController.requestAds(ad.getRequestUrl(), new AdControllerCallbackImpl(ad));
    return true;
}