Skip to content

Commit f9616b5

Browse files
Update PlaybackService.cs
1 parent 1b83d63 commit f9616b5

1 file changed

Lines changed: 23 additions & 20 deletions

File tree

‎AudioCuesheetEditor/Services/Audio/PlaybackService.cs‎

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class PlaybackService(IJSRuntime jsRuntime, ISessionStateContainer sessio
3030
private readonly Lock _timerLock = new();
3131
private TimeSpan? _currentPosition;
3232
private DotNetObjectReference<PlaybackService>? _dotNetObjectReference;
33-
private TimeSpan? _audiofileDurationsBeforeCurrentlyPlayingAudiofile;
33+
private TimeSpan _durationBeforeCurrentlyPlayingAudiofile;
3434

3535
public event Action? CurrentPositionChanged;
3636

@@ -72,6 +72,7 @@ public async Task InitializeAsync()
7272
_dotNetObjectReference = DotNetObjectReference.Create(this);
7373
await _jsRuntime.InvokeVoidAsync("audioInterop.register", _dotNetObjectReference);
7474
}
75+
//TODO: When audiofile gets cleared playback doesnt stop but the audioplayer doesn't offer stop anymore
7576
_sessionStateContainer.CuesheetChanged += SessionStateContainer_CuesheetChanged;
7677
}
7778

@@ -229,7 +230,7 @@ public void OnPlaybackPaused()
229230
{
230231
IsPaused = true;
231232
StopTimer();
232-
UpdateCurrentPosition(null);
233+
UpdateCurrentPosition();
233234
}
234235

235236
public async ValueTask DisposeAsync()
@@ -245,14 +246,14 @@ async Task PlayAsync(Audiofile audiofileToPlay)
245246
await _jsRuntime.InvokeVoidAsync("audioInterop.setAudioSource", audiofileToPlay.ObjectURL);
246247
await _jsRuntime.InvokeVoidAsync("audioInterop.playAudio");
247248
_currentlyPlayingAudiofile = audiofileToPlay;
248-
_audiofileDurationsBeforeCurrentlyPlayingAudiofile = null;
249+
_durationBeforeCurrentlyPlayingAudiofile = TimeSpan.Zero;
249250
}
250251

251252
void Reset()
252253
{
253254
StopTimer();
254255
_currentlyPlayingAudiofile = null;
255-
_audiofileDurationsBeforeCurrentlyPlayingAudiofile = null;
256+
_durationBeforeCurrentlyPlayingAudiofile = TimeSpan.Zero;
256257
CurrentPosition = null;
257258
IsPaused = false;
258259
}
@@ -268,40 +269,42 @@ void StopTimer()
268269
_updateTimer = null;
269270
}
270271

271-
async void UpdateCurrentPosition(object? state)
272+
async void UpdateCurrentPosition(object? state = null)
272273
{
273274
lock (_timerLock)
274275
{
275-
if (_currentlyPlayingAudiofile == null)
276+
if (IsPlaying == false)
276277
{
277-
StopTimer();
278+
Reset();
279+
return;
280+
}
281+
else
282+
{
283+
SetDurationBeforeCurrentlyPlayingAudiofile();
278284
}
279285
}
280-
CalculateDurationsBeforeCurrentlyPlayingAudiofile();
281-
//TODO: When audio is stopped the current position seems to be 00:00:00 and not --:--:--
282-
var currentSecondsInCurrentlyPlayingAudiofile = await _jsRuntime.InvokeAsync<double>("audioInterop.getAudioCurrentTime");
283-
if (_audiofileDurationsBeforeCurrentlyPlayingAudiofile.HasValue)
284-
{
285-
CurrentPosition = _audiofileDurationsBeforeCurrentlyPlayingAudiofile + TimeSpan.FromSeconds(currentSecondsInCurrentlyPlayingAudiofile);
286-
}
287-
else
286+
if (IsPlaying)
288287
{
289-
CurrentPosition = TimeSpan.FromSeconds(currentSecondsInCurrentlyPlayingAudiofile);
288+
var currentSecondsInCurrentlyPlayingAudiofile = await _jsRuntime.InvokeAsync<double>("audioInterop.getAudioCurrentTime");
289+
CurrentPosition = _durationBeforeCurrentlyPlayingAudiofile + TimeSpan.FromSeconds(currentSecondsInCurrentlyPlayingAudiofile);
290290
}
291291
}
292292

293-
void CalculateDurationsBeforeCurrentlyPlayingAudiofile()
293+
void SetDurationBeforeCurrentlyPlayingAudiofile()
294294
{
295-
if ((_audiofileDurationsBeforeCurrentlyPlayingAudiofile != null) || (_currentlyPlayingAudiofile == null))
295+
if (_currentlyPlayingAudiofile == null)
296296
{
297297
return;
298298
}
299-
_audiofileDurationsBeforeCurrentlyPlayingAudiofile = TimeSpan.Zero;
299+
_durationBeforeCurrentlyPlayingAudiofile = TimeSpan.Zero;
300300
var index = _sessionStateContainer.Cuesheet.Audiofiles.IndexOf(_currentlyPlayingAudiofile);
301301
for (int i = 0; i < index; i++)
302302
{
303303
var audiofile = _sessionStateContainer.Cuesheet.Audiofiles[i];
304-
_audiofileDurationsBeforeCurrentlyPlayingAudiofile += audiofile.Duration;
304+
if (audiofile.Duration.HasValue)
305+
{
306+
_durationBeforeCurrentlyPlayingAudiofile += audiofile.Duration.Value;
307+
}
305308
}
306309
}
307310
}

0 commit comments

Comments
 (0)