Esempio n. 1
0
        private void PlayVideo(float startTime)
        {
            if (VideoConfig == null)
            {
                Log.Warn("VideoConfig null in PlayVideo");
                return;
            }

            VideoPlayer.IsSyncing = false;

            // Always hide screen body in the menu, since the drawbacks of the body being visible are large
            if (VideoConfig.TransparencyEnabled && _activeScene != Scene.Menu)
            {
                VideoPlayer.ShowScreenBody();
            }
            else
            {
                VideoPlayer.HideScreenBody();
            }

            var totalOffset = VideoConfig.GetOffsetInSec();
            var songSpeed   = 1f;

            if (BS_Utils.Plugin.LevelData.IsSet)
            {
                songSpeed = BS_Utils.Plugin.LevelData.GameplayCoreSceneSetupData.gameplayModifiers.songSpeedMul;

                if (BS_Utils.Plugin.LevelData.GameplayCoreSceneSetupData?.practiceSettings != null)
                {
                    songSpeed = BS_Utils.Plugin.LevelData.GameplayCoreSceneSetupData.practiceSettings.songSpeedMul;
                    if ((totalOffset + startTime) < 0)
                    {
                        totalOffset /= (songSpeed * VideoConfig.PlaybackSpeed);
                    }
                }
            }

            VideoPlayer.PlaybackSpeed = songSpeed * VideoConfig.PlaybackSpeed;
            totalOffset += startTime;             //This must happen after song speed adjustment

            if ((songSpeed * VideoConfig.PlaybackSpeed) < 1f && totalOffset > 0f)
            {
                //Unity crashes if the playback speed is less than 1 and the video time at the start of playback is greater than 0
                Log.Warn("Video playback disabled to prevent Unity crash");
                VideoPlayer.Hide();
                StopPlayback();
                return;
            }

            //Video seemingly always lags behind. A fixed offset seems to work well enough
            if (!IsPreviewPlaying)
            {
                totalOffset += 0.0667f;
            }

            if (VideoConfig.endVideoAt != null && totalOffset > VideoConfig.endVideoAt)
            {
                totalOffset = VideoConfig.endVideoAt.Value;
            }

            //This will fail if the video is not prepared yet
            if (VideoPlayer.VideoDuration > 0)
            {
                totalOffset %= VideoPlayer.VideoDuration;
            }

            //This fixes an issue where the Unity video player sometimes ignores a change in the .time property if the time is very small and the player is currently playing
            if (Math.Abs(totalOffset) < 0.001f)
            {
                totalOffset = 0;
                Log.Debug("Set very small offset to 0");
            }

            Log.Debug($"Total offset: {totalOffset}, startTime: {startTime}, songSpeed: {songSpeed}, player time: {VideoPlayer.Player.time}");

            StopAllCoroutines();

            if (_activeAudioSource != null)
            {
                _lastKnownAudioSourceTime = _activeAudioSource.time;
            }

            if (totalOffset < 0)
            {
                if (!IsPreviewPlaying)
                {
                    //Negate the offset to turn it into a positive delay
                    StartCoroutine(PlayVideoDelayedCoroutine(-(totalOffset)));
                }
                else
                {
                    //In menus we don't need to wait, instead the preview player starts earlier
                    VideoPlayer.Play();
                }
            }
            else
            {
                VideoPlayer.Play();
                if (!VideoPlayer.Player.isPrepared)
                {
                    _audioSourceStartTime = DateTime.Now;
                    _offsetAfterPrepare   = totalOffset;
                }
                else
                {
                    VideoPlayer.Player.time = totalOffset;
                }
            }
        }
Esempio n. 2
0
        public async void StartPreview()
        {
            if (VideoConfig == null || _currentLevel == null)
            {
                Log.Warn("No video or level selected in OnPreviewAction");
                return;
            }
            if (IsPreviewPlaying)
            {
                Log.Debug("Stopping preview");
                StopPreview(true);
            }
            else
            {
                Log.Debug("Starting preview");
                IsPreviewPlaying = true;

                if (VideoPlayer.IsPlaying)
                {
                    StopPlayback();
                }

                if (!VideoPlayer.IsPrepared)
                {
                    Log.Debug("Video not prepared yet");
                }

                //Start the preview at the point the video kicks in
                var startTime = 0f;
                if (VideoConfig.offset < 0)
                {
                    startTime = -VideoConfig.GetOffsetInSec();
                }

                if (SongPreviewPlayerController.SongPreviewPlayer == null)
                {
                    Log.Error("Failed to get reference to SongPreviewPlayer during preview");
                    return;
                }

                try
                {
                    Log.Debug($"Preview start time: {startTime}, offset: {VideoConfig.GetOffsetInSec()}");
                    var audioClip = await VideoLoader.GetAudioClipForLevel(_currentLevel);

                    if (audioClip != null)
                    {
                        SongPreviewPlayerController.SongPreviewPlayer.CrossfadeTo(audioClip, -5f, startTime, _currentLevel.songDuration, null);
                    }
                    else
                    {
                        Log.Error("AudioClip for level failed to load");
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e);
                    IsPreviewPlaying = false;
                    return;
                }

                //+1.0 is hard right. only pan "mostly" right, because for some reason the video player audio doesn't
                //pan hard left either. Also, it sounds a bit more comfortable.
                SetAudioSourcePanning(0.9f);
                StartCoroutine(PlayVideoAfterAudioSourceCoroutine(true));
                VideoPlayer.PanStereo = -1f;                 // -1 is hard left
                VideoPlayer.Unmute();
            }
        }