Skip to content

Commit 49c2182

Browse files
Fix audio jumping issue
1 parent a39c137 commit 49c2182

File tree

5 files changed

+20
-3
lines changed

5 files changed

+20
-3
lines changed

MSUScripter/MSUScripter.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
99
<ApplicationIcon>MSUScripterIcon.ico</ApplicationIcon>
1010
<PackageIcon>MSUScripterIcon.ico</PackageIcon>
11-
<Version>4.0.0-beta.2</Version>
11+
<Version>4.0.0-beta.3</Version>
1212
<RuntimeFrameworkVersion>8.0.0</RuntimeFrameworkVersion>
1313
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
1414
<LangVersion>12</LangVersion>

MSUScripter/Services/ControlServices/AudioControlService.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ public void PlayPause()
5757

5858
public void UpdatePosition(double position)
5959
{
60-
audioService.SetPosition(position / 100);
60+
if (Math.Abs(position - _model.PreviousPosition) > 0.01)
61+
{
62+
_model.PreviousPosition = position;
63+
audioService.SetPosition(position / 100);
64+
}
6165
}
6266

6367
public void SetSeconds()
@@ -81,6 +85,12 @@ public void UpdateVolume(double volume)
8185
settingsService.SaveSettings();
8286
}
8387

88+
public void ShutdownService()
89+
{
90+
_timer.Elapsed -= TimerOnElapsed;
91+
_timer.Stop();
92+
}
93+
8494
private void PlayStopped(object? sender, EventArgs e)
8595
{
8696
_model.Icon = MaterialIconKind.Stop;
@@ -120,7 +130,7 @@ private void StopTimer()
120130

121131
private void TimerOnElapsed(object? sender, ElapsedEventArgs e)
122132
{
123-
_model.Position = (audioService.GetCurrentPosition() ?? 0.0) * 100;
133+
_model.Position = _model.PreviousPosition = (audioService.GetCurrentPosition() ?? 0.0) * 100;
124134
var currentTime = TimeSpan.FromSeconds(audioService.GetCurrentPositionSeconds()).ToString(@"mm\:ss");
125135
var totalTime = TimeSpan.FromSeconds(audioService.GetLengthSeconds()).ToString(@"mm\:ss");
126136
_model.Timestamp = $"{currentTime}/{totalTime}";

MSUScripter/ViewModels/AudioControlViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class AudioControlViewModel : ViewModelBase
1717
[Reactive] public bool CanPopout { get; set; }
1818
[Reactive] public bool CanSetTimeSeconds { get; set; }
1919
[Reactive] public bool CanPressPopoutButton { get; set; } = true;
20+
public double PreviousPosition { get; set; }
2021
public int IconSize => 16;
2122

2223
public override ViewModelBase DesignerExample()

MSUScripter/Views/AudioControl.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
xmlns:controls="clr-namespace:AvaloniaControls.Controls;assembly=AvaloniaControls"
88
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="40"
99
x:Class="MSUScripter.Views.AudioControl"
10+
Unloaded="Control_OnUnloaded"
1011
x:DataType="viewModels:AudioControlViewModel">
1112
<Grid VerticalAlignment="Center" ColumnDefinitions="Auto,*,Auto,Auto" IsVisible="{Binding CanPlayMusic}">
1213

MSUScripter/Views/AudioControl.axaml.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,9 @@ private void VolumeSlider_OnLoaded(object? sender, RoutedEventArgs e)
102102
if (_service == null || sender is not Slider slider) return;
103103
slider.Value = _service.GetCurrentVolume();
104104
}
105+
106+
private void Control_OnUnloaded(object? sender, RoutedEventArgs e)
107+
{
108+
_service?.ShutdownService();
109+
}
105110
}

0 commit comments

Comments
 (0)