Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio player remove improvements #443

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions FModel/Settings/UserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ public Hotkey AddAudio
set => SetProperty(ref _addAudio, value);
}

private Hotkey _removeAudio = new(Key.X);
public Hotkey RemoveAudio
{
get => _removeAudio;
set => SetProperty(ref _removeAudio, value);
}

private Hotkey _playPauseAudio = new(Key.K);
public Hotkey PlayPauseAudio
{
Expand Down
38 changes: 38 additions & 0 deletions FModel/ViewModels/AudioPlayerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,20 @@ public void Load()
});
}

public void Unload()
{
Application.Current.Dispatcher.Invoke(() =>
{
_waveSource = null;

PlayedFile = new AudioFile(-1, "No audio file");
Spectrum = null;

RaiseSourceEvent(ESourceEventType.Clearing);
ClearSoundOut();
});
}

public void AddToPlaylist(byte[] data, string filePath)
{
Application.Current.Dispatcher.Invoke(() =>
Expand Down Expand Up @@ -265,11 +279,30 @@ public void Remove()
if (_audioFiles.Count < 1) return;
Application.Current.Dispatcher.Invoke(() =>
{
var removedPlaying = false;
if (PlayedFile.Id == SelectedAudioFile.Id)
{
removedPlaying = true;
Stop();
}

_audioFiles.RemoveAt(SelectedAudioFile.Id);
for (var i = 0; i < _audioFiles.Count; i++)
{
_audioFiles[i].Id = i;
}

if (_audioFiles.Count < 1)
{
Unload();
return;
}

SelectedAudioFile = _audioFiles[SelectedAudioFile.Id];

if (!removedPlaying) return;
Load();
Play();
});
}

Expand Down Expand Up @@ -501,6 +534,11 @@ private void LoadSoundOut()
_soundOut.Volume = UserSettings.Default.AudioPlayerVolume / 100;
}

private void ClearSoundOut()
{
_soundOut = null;
}

private IEnumerable<MMDevice> EnumerateDevices()
{
using var deviceEnumerator = new MMDeviceEnumerator();
Expand Down
2 changes: 2 additions & 0 deletions FModel/Views/AudioPlayer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ private void OnPreviewKeyDown(object sender, KeyEventArgs e)
_applicationView.AudioPlayer.Previous();
else if (UserSettings.Default.NextAudio.IsTriggered(e.Key))
_applicationView.AudioPlayer.Next();
else if (UserSettings.Default.RemoveAudio.IsTriggered(e.Key))
_applicationView.AudioPlayer.Remove();
}

private void OnAudioFileMouseDoubleClick(object sender, MouseButtonEventArgs e)
Expand Down
5 changes: 3 additions & 2 deletions FModel/Views/Resources/Controls/Aup/SourceEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ namespace FModel.Views.Resources.Controls.Aup;

public enum ESourceEventType
{
Loading
Loading,
Clearing
}

public class SourceEventArgs : EventArgs
Expand All @@ -15,4 +16,4 @@ public SourceEventArgs(ESourceEventType e)
{
Event = e;
}
}
}
4 changes: 4 additions & 0 deletions FModel/Views/SettingsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
Expand Down Expand Up @@ -483,6 +484,9 @@
<TextBlock Grid.Row="11" Grid.Column="0" Text="Next Audio" VerticalAlignment="Center" Margin="0 0 0 5" />
<controls:HotkeyTextBox Grid.Row="11" Grid.Column="2" Style="{StaticResource TextBoxDefaultStyle}" Margin="0 0 0 5"
HotKey="{Binding NextAudio, Source={x:Static local:Settings.UserSettings.Default}, Mode=TwoWay}" />
<TextBlock Grid.Row="12" Grid.Column="0" Text="Remove Selected Audio" VerticalAlignment="Center" Margin="0 0 0 5" />
<controls:HotkeyTextBox Grid.Row="12" Grid.Column="2" Style="{StaticResource TextBoxDefaultStyle}" Margin="0 0 0 5"
HotKey="{Binding RemoveAudio, Source={x:Static local:Settings.UserSettings.Default}, Mode=TwoWay}" />
</Grid>
</DataTemplate>
</ResourceDictionary>
Expand Down