コード例 #1
0
  public void playVideo(String fileName, boolean cached) {
    if (fileName == null) return;

    _videoPlayheadPrepared = false;
    UnityAdsDeviceLog.debug("Playing video from: " + fileName);

    _videoView.setOnErrorListener(
        new MediaPlayer.OnErrorListener() {
          @Override
          public boolean onError(MediaPlayer mp, int what, int extra) {
            UnityAdsDeviceLog.error(
                "For some reason the device failed to play the video (error: "
                    + what
                    + ", "
                    + extra
                    + "), a crash was prevented.");
            videoPlaybackFailed();
            return true;
          }
        });

    // Set the video file to readable, in some caching cases the MediaPlayer cannot play the file
    // unless it's set to readable for all
    if (cached) {
      File f = new File(fileName);
      boolean result = f.setReadable(true, false);
      if (!result) UnityAdsDeviceLog.debug("COULD NOT SET FILE READABLE");
    }

    try {
      _videoView.setVideoPath(fileName);
    } catch (Exception e) {
      UnityAdsDeviceLog.error(
          "For some reason the device failed to play the video, a crash was prevented.");
      videoPlaybackFailed();
      return;
    }

    if (!_videoPlaybackErrors) {
      updateTimeLeftText();
      _bufferingStartedMillis = System.currentTimeMillis();
      startVideo();
    }
  }
コード例 #2
0
  private void storeVolume() {
    AudioManager am =
        ((AudioManager)
            UnityAdsProperties.APPLICATION_CONTEXT.getSystemService(Context.AUDIO_SERVICE));
    int curVol;
    int maxVol;

    if (am != null) {
      curVol = am.getStreamVolume(AudioManager.STREAM_MUSIC);
      maxVol = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
      float parts = 1f / (float) maxVol;
      _volumeBeforeMute = parts * (float) curVol;
      UnityAdsDeviceLog.debug(
          "Storing volume: " + curVol + ", " + maxVol + ", " + parts + ", " + _volumeBeforeMute);
    }
  }
コード例 #3
0
  public void clearVideoPlayer() {
    UnityAdsDeviceLog.entered();
    setKeepScreenOn(false);
    setOnClickListener(null);
    setOnFocusChangeListener(null);

    hideSkipText();
    hideTimeRemainingLabel();
    hideVideoPausedView();
    purgeVideoPausedTimer();
    _videoView.stopPlayback();
    _videoView.setOnCompletionListener(null);
    _videoView.setOnPreparedListener(null);
    _videoView.setOnErrorListener(null);

    removeAllViews();
  }
コード例 #4
0
    @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);
                  }
                }
              }
            });
      }
    }