diff --git a/Correlator/Util/MethodExtensions.cs b/Correlator/Util/MethodExtensions.cs index fb32d28..20ca87b 100644 --- a/Correlator/Util/MethodExtensions.cs +++ b/Correlator/Util/MethodExtensions.cs @@ -14,6 +14,7 @@ using Correlator.SensorHubTag; using HandyControl.Controls; using MathWorks.MATLAB.NET.Arrays; +using NAudio.Wave; using Tag = Correlator.SensorHubTag.Tag; namespace Correlator.Util @@ -539,7 +540,27 @@ return bitmap; } - + + /// + /// 采用流的方式加载图片资源,防止资源被一直占用 + /// + /// + /// + public static string GetAudioDuration(this string audioPath) + { + var duration = "--:--"; + if (File.Exists(audioPath)) + { + using (var reader = new AudioFileReader(audioPath)) + { + var timeSpan = reader.TotalTime; + duration = timeSpan.ToString(@"hh\:mm\:ss"); + } + } + + return duration; + } + /// /// List 转 ObservableCollection /// diff --git a/Correlator/Util/MethodExtensions.cs b/Correlator/Util/MethodExtensions.cs index fb32d28..20ca87b 100644 --- a/Correlator/Util/MethodExtensions.cs +++ b/Correlator/Util/MethodExtensions.cs @@ -14,6 +14,7 @@ using Correlator.SensorHubTag; using HandyControl.Controls; using MathWorks.MATLAB.NET.Arrays; +using NAudio.Wave; using Tag = Correlator.SensorHubTag.Tag; namespace Correlator.Util @@ -539,7 +540,27 @@ return bitmap; } - + + /// + /// 采用流的方式加载图片资源,防止资源被一直占用 + /// + /// + /// + public static string GetAudioDuration(this string audioPath) + { + var duration = "--:--"; + if (File.Exists(audioPath)) + { + using (var reader = new AudioFileReader(audioPath)) + { + var timeSpan = reader.TotalTime; + duration = timeSpan.ToString(@"hh\:mm\:ss"); + } + } + + return duration; + } + /// /// List 转 ObservableCollection /// diff --git a/Correlator/ViewModels/AudioFileDialogViewModel.cs b/Correlator/ViewModels/AudioFileDialogViewModel.cs index 6de901e..46b7dbf 100644 --- a/Correlator/ViewModels/AudioFileDialogViewModel.cs +++ b/Correlator/ViewModels/AudioFileDialogViewModel.cs @@ -3,6 +3,7 @@ using System.Collections.ObjectModel; using System.IO; using System.Linq; +using System.Threading.Tasks; using System.Windows; using System.Windows.Media.Imaging; using System.Windows.Threading; @@ -42,7 +43,7 @@ RaisePropertyChanged(); } } - + private ObservableCollection _audioFiles; public ObservableCollection AudioFiles @@ -103,6 +104,18 @@ } } + private bool _taskHasCompleted; + + public bool TaskHasCompleted + { + get => _taskHasCompleted; + set + { + _taskHasCompleted = value; + RaisePropertyChanged(); + } + } + #endregion #region DelegateCommand @@ -122,7 +135,7 @@ /// 列表每页条目数 /// private const int PerPageItemCount = 8; - + private readonly DispatcherTimer _audioTimer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 1) @@ -193,7 +206,7 @@ } } - private void DeleteAudio() + private async void DeleteAudio() { if (_selectedFile == null) { @@ -212,7 +225,7 @@ File.Delete(_selectedFile.FullPath); //刷新数据 - var totalFiles = GetTotalAudioFiles(); + var totalFiles = await GetTotalAudioFilesAsync(); MaxPage = (totalFiles.Count + PerPageItemCount - 1) / PerPageItemCount; AudioFiles = totalFiles.ToObservableCollection(); } @@ -222,9 +235,10 @@ } } - private void PageUpdated(FunctionEventArgs args) + private async void PageUpdated(FunctionEventArgs args) { - var audioFiles = GetTotalAudioFiles() + var files = await GetTotalAudioFilesAsync(); + var audioFiles = files .Skip((args.Info - 1) * PerPageItemCount) .Take(PerPageItemCount) .ToList(); @@ -247,18 +261,35 @@ { } - public void OnDialogOpened(IDialogParameters parameters) + public async void OnDialogOpened(IDialogParameters parameters) { EmptyBitmapImage = new BitmapImage(new Uri(@"..\..\Image\empty_image.png", UriKind.Relative)); - var totalFiles = GetTotalAudioFiles(); + var totalFiles = await GetTotalAudioFilesAsync(); MaxPage = (totalFiles.Count + PerPageItemCount - 1) / PerPageItemCount; AudioFiles = totalFiles.Take(PerPageItemCount).ToList().ToObservableCollection(); } - private static List GetTotalAudioFiles() + /// + /// 异步遍历文件夹,防止卡主线程 + /// + /// + private async Task> GetTotalAudioFilesAsync() { var audioFiles = new List(); - var files = new DirectoryInfo(DirectoryManager.GetAudioDir()) + + await Task.Run(() => TraverseFolder(DirectoryManager.GetAudioDir(), audioFiles)); + TaskHasCompleted = true; + return audioFiles.OrderBy(file => file.CreationTime).Reverse().ToList(); + } + + /// + /// 遍历文件夹并生成相应的数据类型集合 + /// + /// + /// + private static void TraverseFolder(string folderPath, List audioFiles) + { + var files = new DirectoryInfo(folderPath) .GetFiles("*", SearchOption.AllDirectories) .OrderBy(file => file.CreationTime) .Reverse(); @@ -277,14 +308,13 @@ { FileName = fileName, FileSource = fileSource, - FullPath = DirectoryManager.GetAudioDir() + "\\" + fileName, + FullPath = file.FullName, FileSize = file.Length.FormatFileSize(), + Duration = file.FullName.GetAudioDuration(), CreationTime = file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss") }); } } - - return audioFiles; } } } \ No newline at end of file