@@ -25,6 +25,7 @@ public sealed class DspSongClockPrototype : MonoBehaviour
2525 private string externalAudioPath ;
2626 private double audioPlaybackSpeed = 1.0 ;
2727 private UnityWebRequest audioRequest ;
28+ private bool seekedWhilePaused ;
2829
2930 public DspSongClock Clock { get ; private set ; }
3031 public double StartDspTime => Clock != null && Clock . IsScheduled ? Clock . StartDspTime : double . NaN ;
@@ -142,7 +143,15 @@ public void ResumePlayback()
142143 {
143144 if ( Clock == null || ! Clock . IsPaused ) return ;
144145 Clock . Resume ( ) ;
145- audioSource . UnPause ( ) ;
146+ if ( seekedWhilePaused )
147+ {
148+ audioSource . Play ( ) ;
149+ seekedWhilePaused = false ;
150+ }
151+ else
152+ {
153+ audioSource . UnPause ( ) ;
154+ }
146155 }
147156
148157 public void RestartPlayback ( )
@@ -154,6 +163,35 @@ public void RestartPlayback()
154163 completedLogged = false ;
155164 }
156165
166+ public void SeekPlayback ( double positionSeconds )
167+ {
168+ if ( generatedClip == null || Clock == null || ! Clock . IsScheduled )
169+ throw new InvalidOperationException ( "Song playback must be scheduled before seeking." ) ;
170+ if ( double . IsNaN ( positionSeconds ) || double . IsInfinity ( positionSeconds ) ||
171+ positionSeconds < 0 || positionSeconds >= Clock . DurationSeconds )
172+ throw new ArgumentOutOfRangeException ( nameof ( positionSeconds ) ) ;
173+
174+ bool wasPaused = Clock . IsPaused ;
175+ double clipPosition = Math . Min (
176+ generatedClip . length - ( 1.0 / Math . Max ( 1 , generatedClip . frequency ) ) ,
177+ positionSeconds * audioPlaybackSpeed ) ;
178+
179+ audioSource . Stop ( ) ;
180+ audioSource . time = ( float ) Math . Max ( 0 , clipPosition ) ;
181+ Clock . Seek ( positionSeconds ) ;
182+ if ( wasPaused )
183+ {
184+ seekedWhilePaused = true ;
185+ }
186+ else
187+ {
188+ audioSource . Play ( ) ;
189+ seekedWhilePaused = false ;
190+ }
191+ startedLogged = positionSeconds >= 0 ;
192+ completedLogged = false ;
193+ }
194+
157195 public void PreviewFromSourceTime ( double sourceTimeSeconds , double speed = 1.0 )
158196 {
159197 if ( generatedClip == null || audioSource == null )
@@ -186,6 +224,7 @@ private void SchedulePlayback()
186224 Clock = new DspSongClock ( timeSource ) ;
187225 Clock . Schedule ( startDspTime , generatedClip . length / audioPlaybackSpeed ) ;
188226 audioSource . PlayScheduled ( startDspTime ) ;
227+ seekedWhilePaused = false ;
189228 }
190229
191230 private void Update ( )
0 commit comments