private void setBufferingTextVisibility( final int visibility, final boolean hasSkip, final boolean canSkip) { UnityAdsUtils.runOnUiThread( new Runnable() { @Override public void run() { if (_bufferingText != null) { _bufferingText.setVisibility(visibility); } if (visibility == VISIBLE) { if (_skipTextView == null) { updateSkipText(getSkipDuration()); } enableSkippingFromSkipText(); } else { if (hasSkip) { if (canSkip) { enableSkippingFromSkipText(); } else { disableSkippingFromSkipText(); } } else { hideSkipText(); } } } }); }
private void startVideo() { UnityAdsUtils.runOnUiThread( new Runnable() { @Override public void run() { _videoView.start(); setKeepScreenOn(true); } }); if (_videoPausedTimer == null) { _videoPausedTimer = new Timer(); _videoPausedTimer.scheduleAtFixedRate(new VideoStateChecker(), 500, 500); } }
public void pauseVideo() { purgeVideoPausedTimer(); if (_videoView != null && _videoView.isPlaying()) { UnityAdsUtils.runOnUiThread( new Runnable() { @Override public void run() { _videoView.pause(); setKeepScreenOn(false); createAndAddPausedView(); } }); } }
@Override public void run() { if (_videoView == null || _timeLeftInSecondsText == null) { purgeVideoPausedTimer(); return; } _oldPos = _curPos; try { _curPos = (float) _videoView.getCurrentPosition(); } catch (Exception e) { UnityAdsDeviceLog.error("Could not get videoView currentPosition"); if (_oldPos > 0) _curPos = _oldPos; else _curPos = 0.01f; } Float position; int duration = 1; Boolean durationSuccess = true; try { duration = _videoView.getDuration(); } catch (Exception e) { UnityAdsDeviceLog.error("Could not get videoView duration"); durationSuccess = false; } if (durationSuccess) _duration = duration; position = _curPos / _duration; if (_curPos > _oldPos) { _playHeadHasMoved = true; _videoHasStalled = false; setBufferingTextVisibility(INVISIBLE, hasSkipDuration(), _skipTimeLeft <= 0f); } else { _videoHasStalled = true; setBufferingTextVisibility(VISIBLE, true, true); } UnityAdsUtils.runOnUiThread( new Runnable() { @Override public void run() { updateTimeLeftText(); } }); if (hasSkipDuration() && getSkipDuration() > 0 && _skipTimeLeft > 0f && (_duration / 1000) > getSkipDuration()) { _skipTimeLeft = (getSkipDuration() * 1000) - _curPos; if (_skipTimeLeft < 0) _skipTimeLeft = 0f; if (_skipTimeLeft == 0) { UnityAdsUtils.runOnUiThread( new Runnable() { @Override public void run() { enableSkippingFromSkipText(); } }); } else { UnityAdsUtils.runOnUiThread( new Runnable() { @Override public void run() { if (_skipTextView != null && !_videoHasStalled) { _skipTextView.setVisibility(VISIBLE); updateSkipText( Math.round(Math.ceil(((getSkipDuration() * 1000) - _curPos) / 1000))); } } }); } } else if (_playHeadHasMoved && (_duration / 1000) <= getSkipDuration()) { UnityAdsUtils.runOnUiThread( new Runnable() { @Override public void run() { hideSkipText(); } }); } if (position > 0.25 && !_sentPositionEvents.containsKey(UnityAdsVideoPosition.FirstQuartile)) { _listener.onEventPositionReached(UnityAdsVideoPosition.FirstQuartile); _sentPositionEvents.put(UnityAdsVideoPosition.FirstQuartile, true); } if (position > 0.5 && !_sentPositionEvents.containsKey(UnityAdsVideoPosition.MidPoint)) { _listener.onEventPositionReached(UnityAdsVideoPosition.MidPoint); _sentPositionEvents.put(UnityAdsVideoPosition.MidPoint, true); } if (position > 0.75 && !_sentPositionEvents.containsKey(UnityAdsVideoPosition.ThirdQuartile)) { _listener.onEventPositionReached(UnityAdsVideoPosition.ThirdQuartile); _sentPositionEvents.put(UnityAdsVideoPosition.ThirdQuartile, true); } if (!_playHeadHasMoved && _bufferingStartedMillis > 0 && (System.currentTimeMillis() - _bufferingStartedMillis) > (UnityAdsProperties.MAX_BUFFERING_WAIT_SECONDS * 1000)) { this.cancel(); UnityAdsUtils.runOnUiThread( new Runnable() { @Override public void run() { UnityAdsDeviceLog.error("Buffering taking too long.. cancelling video play"); videoPlaybackFailed(); } }); } if (_videoPlayheadPrepared && _playHeadHasMoved) { UnityAdsUtils.runOnUiThread( new Runnable() { @Override public void run() { if (!_videoPlaybackStartedSent) { if (_listener != null) { _videoPlaybackStartedSent = true; _listener.onVideoPlaybackStarted(); _videoStartedPlayingMillis = System.currentTimeMillis(); } if (!_sentPositionEvents.containsKey(UnityAdsVideoPosition.Start)) { _sentPositionEvents.put(UnityAdsVideoPosition.Start, true); _listener.onEventPositionReached(UnityAdsVideoPosition.Start); } } } }); } }