diff --git a/Correlator/ViewModels/AudioFileViewModel.cs b/Correlator/ViewModels/AudioFileViewModel.cs index 0d7cf3d..bd5345e 100644 --- a/Correlator/ViewModels/AudioFileViewModel.cs +++ b/Correlator/ViewModels/AudioFileViewModel.cs @@ -6,6 +6,7 @@ using Correlator.Util; using HandyControl.Controls; using HandyControl.Data; +using NAudio.Wave; using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; @@ -62,6 +63,7 @@ public DelegateCommand GoBackCommand { get; set; } public DelegateCommand ItemSelectedCommand { get; set; } + public DelegateCommand PlayAudioCommand { get; set; } public DelegateCommand DeleteItemCommand { get; set; } public DelegateCommand ReLoadDataCommand { get; set; } public DelegateCommand> PageUpdatedCmd { get; set; } @@ -69,11 +71,24 @@ #endregion private FileInfo _selectedFile; - + private WaveOutEvent _waveOutEvent = new WaveOutEvent(); + private AudioFileReader _audioFileReader; + public AudioFileViewModel() { GoBackCommand = new DelegateCommand(delegate { + if (_audioFileReader != null) + { + _audioFileReader.Dispose(); + _audioFileReader = null; + } + + if (_waveOutEvent != null) + { + _waveOutEvent.Dispose(); + _waveOutEvent = null; + } RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); }); @@ -82,6 +97,20 @@ _selectedFile = (FileInfo)dataGrid.SelectedItem; }); + PlayAudioCommand = new DelegateCommand(delegate(string fullPath) + { + if (_waveOutEvent.PlaybackState == PlaybackState.Playing) + { + _waveOutEvent.Stop(); + } + else + { + _audioFileReader = new AudioFileReader(fullPath); + _waveOutEvent.Init(_audioFileReader); + _waveOutEvent.Play(); + } + }); + DeleteItemCommand = new DelegateCommand(() => { File.Delete(_selectedFile.FullPath); diff --git a/Correlator/ViewModels/AudioFileViewModel.cs b/Correlator/ViewModels/AudioFileViewModel.cs index 0d7cf3d..bd5345e 100644 --- a/Correlator/ViewModels/AudioFileViewModel.cs +++ b/Correlator/ViewModels/AudioFileViewModel.cs @@ -6,6 +6,7 @@ using Correlator.Util; using HandyControl.Controls; using HandyControl.Data; +using NAudio.Wave; using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; @@ -62,6 +63,7 @@ public DelegateCommand GoBackCommand { get; set; } public DelegateCommand ItemSelectedCommand { get; set; } + public DelegateCommand PlayAudioCommand { get; set; } public DelegateCommand DeleteItemCommand { get; set; } public DelegateCommand ReLoadDataCommand { get; set; } public DelegateCommand> PageUpdatedCmd { get; set; } @@ -69,11 +71,24 @@ #endregion private FileInfo _selectedFile; - + private WaveOutEvent _waveOutEvent = new WaveOutEvent(); + private AudioFileReader _audioFileReader; + public AudioFileViewModel() { GoBackCommand = new DelegateCommand(delegate { + if (_audioFileReader != null) + { + _audioFileReader.Dispose(); + _audioFileReader = null; + } + + if (_waveOutEvent != null) + { + _waveOutEvent.Dispose(); + _waveOutEvent = null; + } RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); }); @@ -82,6 +97,20 @@ _selectedFile = (FileInfo)dataGrid.SelectedItem; }); + PlayAudioCommand = new DelegateCommand(delegate(string fullPath) + { + if (_waveOutEvent.PlaybackState == PlaybackState.Playing) + { + _waveOutEvent.Stop(); + } + else + { + _audioFileReader = new AudioFileReader(fullPath); + _waveOutEvent.Init(_audioFileReader); + _waveOutEvent.Play(); + } + }); + DeleteItemCommand = new DelegateCommand(() => { File.Delete(_selectedFile.FullPath); diff --git a/Correlator/Views/AudioFileView.xaml b/Correlator/Views/AudioFileView.xaml index a3b01e3..324c2ae 100644 --- a/Correlator/Views/AudioFileView.xaml +++ b/Correlator/Views/AudioFileView.xaml @@ -111,9 +111,9 @@ HorizontalAlignment="Left" Background="Transparent" BorderThickness="0" - Click="PlayAudioButton_OnClick" - Style="{x:Null}" - Tag="{Binding FullPath}"> + Command="{Binding DataContext.PlayAudioCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" + CommandParameter="{Binding FullPath}" + Style="{x:Null}"> diff --git a/Correlator/ViewModels/AudioFileViewModel.cs b/Correlator/ViewModels/AudioFileViewModel.cs index 0d7cf3d..bd5345e 100644 --- a/Correlator/ViewModels/AudioFileViewModel.cs +++ b/Correlator/ViewModels/AudioFileViewModel.cs @@ -6,6 +6,7 @@ using Correlator.Util; using HandyControl.Controls; using HandyControl.Data; +using NAudio.Wave; using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; @@ -62,6 +63,7 @@ public DelegateCommand GoBackCommand { get; set; } public DelegateCommand ItemSelectedCommand { get; set; } + public DelegateCommand PlayAudioCommand { get; set; } public DelegateCommand DeleteItemCommand { get; set; } public DelegateCommand ReLoadDataCommand { get; set; } public DelegateCommand> PageUpdatedCmd { get; set; } @@ -69,11 +71,24 @@ #endregion private FileInfo _selectedFile; - + private WaveOutEvent _waveOutEvent = new WaveOutEvent(); + private AudioFileReader _audioFileReader; + public AudioFileViewModel() { GoBackCommand = new DelegateCommand(delegate { + if (_audioFileReader != null) + { + _audioFileReader.Dispose(); + _audioFileReader = null; + } + + if (_waveOutEvent != null) + { + _waveOutEvent.Dispose(); + _waveOutEvent = null; + } RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); }); @@ -82,6 +97,20 @@ _selectedFile = (FileInfo)dataGrid.SelectedItem; }); + PlayAudioCommand = new DelegateCommand(delegate(string fullPath) + { + if (_waveOutEvent.PlaybackState == PlaybackState.Playing) + { + _waveOutEvent.Stop(); + } + else + { + _audioFileReader = new AudioFileReader(fullPath); + _waveOutEvent.Init(_audioFileReader); + _waveOutEvent.Play(); + } + }); + DeleteItemCommand = new DelegateCommand(() => { File.Delete(_selectedFile.FullPath); diff --git a/Correlator/Views/AudioFileView.xaml b/Correlator/Views/AudioFileView.xaml index a3b01e3..324c2ae 100644 --- a/Correlator/Views/AudioFileView.xaml +++ b/Correlator/Views/AudioFileView.xaml @@ -111,9 +111,9 @@ HorizontalAlignment="Left" Background="Transparent" BorderThickness="0" - Click="PlayAudioButton_OnClick" - Style="{x:Null}" - Tag="{Binding FullPath}"> + Command="{Binding DataContext.PlayAudioCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" + CommandParameter="{Binding FullPath}" + Style="{x:Null}"> diff --git a/Correlator/Views/AudioFileView.xaml.cs b/Correlator/Views/AudioFileView.xaml.cs index c7ff0f5..ec94ae9 100644 --- a/Correlator/Views/AudioFileView.xaml.cs +++ b/Correlator/Views/AudioFileView.xaml.cs @@ -1,24 +1,12 @@ -using System.Media; -using System.Windows; -using System.Windows.Controls; +using System.Windows.Controls; namespace Correlator.Views { public partial class AudioFileView : UserControl { - private readonly SoundPlayer _soundPlayer = new SoundPlayer(); - public AudioFileView() { InitializeComponent(); } - - private void PlayAudioButton_OnClick(object sender, RoutedEventArgs e) - { - var fullPath = (sender as Button)?.Tag.ToString(); - - _soundPlayer.SoundLocation = fullPath; - _soundPlayer.Play(); - } } } \ No newline at end of file