From caa1d0bb06685d16114642fea8f9300b4e7ac7d3 Mon Sep 17 00:00:00 2001 From: MichaelO Date: Tue, 15 Oct 2024 23:23:06 +0800 Subject: [PATCH] updated to v2.11.11 --- Assets/OxGFrame/CHANGELOG.md | 6 ++ .../Runtime/Core/AudioFrame/AudioBase.cs | 19 +++--- .../Runtime/Core/AudioFrame/AudioManager.cs | 4 +- .../Runtime/Core/Implement/MediaBase.cs | 12 +++- .../Runtime/Core/VideoFrame/VideoBase.cs | 58 ++++++++++++++++--- Assets/OxGFrame/package.json | 2 +- 6 files changed, 81 insertions(+), 20 deletions(-) diff --git a/Assets/OxGFrame/CHANGELOG.md b/Assets/OxGFrame/CHANGELOG.md index ee23cdae..d669fdb7 100644 --- a/Assets/OxGFrame/CHANGELOG.md +++ b/Assets/OxGFrame/CHANGELOG.md @@ -1,5 +1,11 @@ # CHANGELOG +## [2.11.11] - 2024-10-15 +- Added SkipToPercent method in VideoBase (You can use a percentage to jump to a specific segment of the video). +- Added SetPlaySpeed in VideoBase. +- Added CurrentRemainingLength in MediaBase. +- Fixed CurrentLength return value in MediaBase. + ## [2.11.10] - 2024-10-08 - Added CoreFrames.USFrame.GetActiveScene() method. - Added CoreFrames.USFrame.SetActiveScene(int index) method. diff --git a/Assets/OxGFrame/MediaFrame/Scripts/Runtime/Core/AudioFrame/AudioBase.cs b/Assets/OxGFrame/MediaFrame/Scripts/Runtime/Core/AudioFrame/AudioBase.cs index 55b93d7a..22088bd9 100644 --- a/Assets/OxGFrame/MediaFrame/Scripts/Runtime/Core/AudioFrame/AudioBase.cs +++ b/Assets/OxGFrame/MediaFrame/Scripts/Runtime/Core/AudioFrame/AudioBase.cs @@ -88,7 +88,7 @@ private async UniTask _InitAudio() this._audioSource.priority = this.audioType.priority; this._audioSource.outputAudioMixerGroup = this._mixerGroup; this._audioSource.loop = (this.loops == -1) ? true : false; - this._mediaLength = this._currentLength = (this.audioLength > 0) ? this.audioLength : this.audioClip.length; + this._mediaLength = this._currentRemainingLength = (this.audioLength > 0) ? this.audioLength : this.audioClip.length; this.isPrepared = true; @@ -119,10 +119,10 @@ protected override void OnFixedUpdate(float dt = 0f) if (this.IsPaused()) return; - if (this.CurrentLength() > 0f) + if (this.CurrentRemainingLength() > 0f) { - this._currentLength -= dt; - if (this.CurrentLength() <= 0f) + this._currentRemainingLength -= dt; + if (this.CurrentRemainingLength() <= 0f) { if (this._loops >= 0) { @@ -131,12 +131,12 @@ protected override void OnFixedUpdate(float dt = 0f) this._loops--; if (this._loops <= 0) { - this._currentLength = 0; + this._currentRemainingLength = 0; if (this.autoEndToStop) this.StopSelf(); } else this._audioSource.Play(); } - this._currentLength = this.Length(); + this._currentRemainingLength = this.Length(); } } } @@ -219,7 +219,12 @@ public override float Length() public override float CurrentLength() { - return this._currentLength; + return this._mediaLength - this._currentRemainingLength; + } + + public override float CurrentRemainingLength() + { + return this._currentRemainingLength; } public override void OnRelease() diff --git a/Assets/OxGFrame/MediaFrame/Scripts/Runtime/Core/AudioFrame/AudioManager.cs b/Assets/OxGFrame/MediaFrame/Scripts/Runtime/Core/AudioFrame/AudioManager.cs index 518107e7..bde7a97c 100644 --- a/Assets/OxGFrame/MediaFrame/Scripts/Runtime/Core/AudioFrame/AudioManager.cs +++ b/Assets/OxGFrame/MediaFrame/Scripts/Runtime/Core/AudioFrame/AudioManager.cs @@ -383,7 +383,7 @@ private void _Play(AudioBase audBase, int loops, float volume) this.LoadAndPlay(audBase, loops, volume); - Logging.Print(string.Format("Play Audio: {0}, Current Length: {1} (s)", audBase?.mediaName, audBase?.CurrentLength())); + Logging.Print(string.Format("Play Audio: {0}, Current Length: {1} (s)", audBase?.mediaName, audBase?.CurrentRemainingLength())); } /// @@ -532,7 +532,7 @@ private void _Pause(AudioBase audBase) this.ExitAndStop(audBase, true, false); - Logging.Print(string.Format("Pause Audio: {0}, Current Length: {1} (s)", audBase?.mediaName, audBase?.CurrentLength())); + Logging.Print(string.Format("Pause Audio: {0}, Current Length: {1} (s)", audBase?.mediaName, audBase?.CurrentRemainingLength())); } /// diff --git a/Assets/OxGFrame/MediaFrame/Scripts/Runtime/Core/Implement/MediaBase.cs b/Assets/OxGFrame/MediaFrame/Scripts/Runtime/Core/Implement/MediaBase.cs index ea8fafa7..d6ff5c5c 100644 --- a/Assets/OxGFrame/MediaFrame/Scripts/Runtime/Core/Implement/MediaBase.cs +++ b/Assets/OxGFrame/MediaFrame/Scripts/Runtime/Core/Implement/MediaBase.cs @@ -87,7 +87,7 @@ public async UniTask GetFileText() protected bool _isPaused = false; // 是否暫停 protected bool _isInit = false; // 初始標記 (表示確認初始完畢) protected float _mediaLength = 0f; // 影音長度 - protected float _currentLength = 0f; // 影音當前長度 + protected float _currentRemainingLength = 0f; // 影音當前長度 protected Action _endEvent = null; // 停止播放時的事件調用 public bool isPrepared { get; protected set; } = false; // 影音準備好的標記 internal bool isDestroying = false; // 正在被銷毀的標記 @@ -153,6 +153,12 @@ internal void HandleFixedUpdate(float dt) /// public abstract float CurrentLength(); + /// + /// 取得當前影音剩餘長度 -> Remaining time (單位: 秒) + /// + /// + public abstract float CurrentRemainingLength(); + /// /// 設置停止播放時的事件 Callback /// @@ -170,7 +176,7 @@ public virtual void OnRelease() this.assetName = null; this.mediaName = null; this._mediaLength = 0f; - this._currentLength = 0f; + this._currentRemainingLength = 0f; this._endEvent = null; } @@ -184,7 +190,7 @@ public virtual void OnRelease() /// protected virtual void ResetLength() { - this._currentLength = this._mediaLength; + this._currentRemainingLength = this._mediaLength; } /// diff --git a/Assets/OxGFrame/MediaFrame/Scripts/Runtime/Core/VideoFrame/VideoBase.cs b/Assets/OxGFrame/MediaFrame/Scripts/Runtime/Core/VideoFrame/VideoBase.cs index 12f4cf5c..0a4650eb 100644 --- a/Assets/OxGFrame/MediaFrame/Scripts/Runtime/Core/VideoFrame/VideoBase.cs +++ b/Assets/OxGFrame/MediaFrame/Scripts/Runtime/Core/VideoFrame/VideoBase.cs @@ -126,7 +126,7 @@ private async UniTask _InitVideo() break; } - this._mediaLength = this._currentLength = (1f * this._videoPlayer.frameCount / this._videoPlayer.frameRate); + this._mediaLength = this._currentRemainingLength = (1f * this._videoPlayer.frameCount / this._videoPlayer.playbackSpeed / this._videoPlayer.frameRate); this.isPrepared = true; @@ -189,10 +189,10 @@ protected override void OnFixedUpdate(float dt = 0f) if (this.IsPaused()) return; - if (this.CurrentLength() > 0f) + if (this.CurrentRemainingLength() > 0f) { - this._currentLength -= dt; - if (this.CurrentLength() <= 0f) + this._currentRemainingLength -= dt; + if (this.CurrentRemainingLength() <= 0f) { if (this._loops >= 0) { @@ -201,12 +201,12 @@ protected override void OnFixedUpdate(float dt = 0f) this._loops--; if (this._loops <= 0) { - this._currentLength = 0; + this._currentRemainingLength = 0; if (this.autoEndToStop) this.StopSelf(); } else this._videoPlayer.Play(); } - this._currentLength = this.Length(); + this._currentRemainingLength = this.Length(); } } } @@ -285,7 +285,12 @@ public override float Length() public override float CurrentLength() { - return this._currentLength; + return this._mediaLength - this._currentRemainingLength; + } + + public override float CurrentRemainingLength() + { + return this._currentRemainingLength; } public override void OnRelease() @@ -303,6 +308,45 @@ public override void OnRelease() this._targetCamera = null; } + /// + /// Skip range is 0.0 to 1.0 pct + /// + /// + public void SkipToPercent(float pct) + { + if (this._videoPlayer == null) return; + + // Clamp pct to ensure it's between 0.0 and 1.0 + pct = Mathf.Clamp(pct, 0f, 1f); + + var frame = this._videoPlayer.frameCount * pct; + this._videoPlayer.frame = (long)frame; + + // update length infos + this._mediaLength = (1f * this._videoPlayer.frameCount / this._videoPlayer.playbackSpeed / this._videoPlayer.frameRate); + this._currentRemainingLength = this._mediaLength - (1f * frame / this._videoPlayer.playbackSpeed / this._videoPlayer.frameRate); + // offset remaining time + if (this._currentRemainingLength <= 0) + this._currentRemainingLength = 0.1f; + } + + /// + /// Set speed range is 0.01 to float.MaxValue + /// Generally we recommend these rates: 0.25, 0.5, 1.0, 1.25, 1.5, 1.75, 2.0 + /// + /// + public void SetPlaySpeed(float speed) + { + if (this._videoPlayer == null) return; + + // Clamp speed to ensure it does not go below 0.1 + speed = Mathf.Clamp(speed, 0.01f, float.MaxValue); + + this._videoPlayer.playbackSpeed = speed; + // use skip to percent to update current length and media total length + this.SkipToPercent((this._mediaLength - this._currentRemainingLength) / this._mediaLength); + } + public VideoPlayer GetVideoPlayer() { return this._videoPlayer; diff --git a/Assets/OxGFrame/package.json b/Assets/OxGFrame/package.json index 0db0a5a9..beb2d578 100644 --- a/Assets/OxGFrame/package.json +++ b/Assets/OxGFrame/package.json @@ -2,7 +2,7 @@ "name": "com.michaelo.oxgframe", "displayName": "OxGFrame", "description": "The OxGFrame is a framework based on Unity for accelerating game development. Supports multi-platform Win, OSX, Android, iOS, WebGL.", - "version": "2.11.10", + "version": "2.11.11", "unity": "2021.3", "license": "MIT", "samples": [