From c96a719db9e924058bb5612d35599f6c30e921aa Mon Sep 17 00:00:00 2001 From: NeoCoderMatrix86 <40752681+NeoCoderMatrix86@users.noreply.github.com> Date: Fri, 12 May 2023 16:46:32 +0200 Subject: [PATCH] Update AudioPlayer.razor --- AudioCuesheetEditor/Shared/AudioPlayer.razor | 69 +++++++++++--------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/AudioCuesheetEditor/Shared/AudioPlayer.razor b/AudioCuesheetEditor/Shared/AudioPlayer.razor index 798793cf..5d0ca5eb 100644 --- a/AudioCuesheetEditor/Shared/AudioPlayer.razor +++ b/AudioCuesheetEditor/Shared/AudioPlayer.razor @@ -83,6 +83,7 @@ along with Foobar. If not, see HotKeysContext? hotKeysContext; Boolean paused; Audiofile? audioFile; + Cuesheet? cuesheetAttachedToAudiofileChangedEvent; [Parameter] public EventCallback CurrentlyPlayingTrackChanged { get; set; } @@ -104,6 +105,7 @@ along with Foobar. If not, see _sessionStateContainer.Cuesheet.AudioFileChanged -= Cuesheet_AudioFileChanged; } _localizationService.LocalizationChanged -= LocalizationService_LocalizationChanged; + _sessionStateContainer.CuesheetChanged -= SessionStateContainer_CuesheetChanged; } public Boolean PlaybackPossible @@ -203,21 +205,10 @@ along with Foobar. If not, see .Add(Key.MediaStop, OnStopKeyDown); _localizationService.LocalizationChanged += LocalizationService_LocalizationChanged; - - //Also reset the duration, since audio file will change also - TotalTime = null; - _sessionStateContainer.Cuesheet.AudioFileChanged += Cuesheet_AudioFileChanged; - if (_sessionStateContainer.Cuesheet.Audiofile != null) - { - if (_sessionStateContainer.Cuesheet.Audiofile.IsContentStreamLoaded == false) - { - _sessionStateContainer.Cuesheet.Audiofile.ContentStreamLoaded += AudioFile_ContentStreamLoaded; - } - else - { - AnalyseAudioFile(); - } - } + _sessionStateContainer.CuesheetChanged += SessionStateContainer_CuesheetChanged; + cuesheetAttachedToAudiofileChangedEvent = _sessionStateContainer.Cuesheet; + cuesheetAttachedToAudiofileChangedEvent.AudioFileChanged += Cuesheet_AudioFileChanged; + AnalyseCuesheet(); //Setup audio timer audioUpdateTimer = new Timer(500); @@ -376,22 +367,7 @@ along with Foobar. If not, see void Cuesheet_AudioFileChanged(object? sender, EventArgs args) { - if (_sessionStateContainer.Cuesheet.Audiofile != null) - { - if (_sessionStateContainer.Cuesheet.Audiofile.IsContentStreamLoaded) - { - AnalyseAudioFile(); - } - else - { - TotalTime = null; - _sessionStateContainer.Cuesheet.Audiofile.ContentStreamLoaded += AudioFile_ContentStreamLoaded; - } - } - else - { - TotalTime = null; - } + AnalyseCuesheet(); } void AudioFile_ContentStreamLoaded(object? sender, EventArgs args) @@ -417,4 +393,35 @@ along with Foobar. If not, see StateHasChanged(); } } + + void SessionStateContainer_CuesheetChanged(object? sender, EventArgs args) + { + if (cuesheetAttachedToAudiofileChangedEvent != null) + { + cuesheetAttachedToAudiofileChangedEvent.AudioFileChanged -= Cuesheet_AudioFileChanged; + } + AnalyseCuesheet(); + cuesheetAttachedToAudiofileChangedEvent = _sessionStateContainer.Cuesheet; + cuesheetAttachedToAudiofileChangedEvent.AudioFileChanged += Cuesheet_AudioFileChanged; + } + + void AnalyseCuesheet() + { + if (_sessionStateContainer.Cuesheet.Audiofile != null) + { + if (_sessionStateContainer.Cuesheet.Audiofile.IsContentStreamLoaded) + { + AnalyseAudioFile(); + } + else + { + TotalTime = null; + _sessionStateContainer.Cuesheet.Audiofile.ContentStreamLoaded += AudioFile_ContentStreamLoaded; + } + } + else + { + TotalTime = null; + } + } }