Non-blocking MIDI player #1520

This commit is contained in:
ema
2025-01-01 03:42:18 +08:00
parent e4e0fe3fa6
commit e70c23ad75
2 changed files with 7 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
// Copyright © 2024 QL-Win Contributors
// Copyright © 20172025 QL-Win Contributors
//
// This file is part of QuickLook program.
//
@@ -24,9 +24,7 @@ using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
@@ -56,8 +54,6 @@ internal class MidiPlayer : IDisposable, INotifyPropertyChanged
}
}
public event PropertyChangedEventHandler PropertyChanged;
public MidiPlayer(ViewerPanel panle, ContextObject context)
{
_vp = panle;
@@ -76,7 +72,7 @@ internal class MidiPlayer : IDisposable, INotifyPropertyChanged
_outputDevice = null;
}
public void LoadAndPlay(string path, MediaInfoLib info)
public void LoadAndPlay(string path)
{
_midiFile = MidiFile.Read(path);
_vp.metaTitle.Text = Path.GetFileName(path);
@@ -200,13 +196,13 @@ internal class MidiPlayer : IDisposable, INotifyPropertyChanged
}
};
// Playback supported by DryWetMidi will block the current thread
// So we should run it in a new thread
_ = Task.Run(() => _playback?.Play());
_playback.Start();
_vp.buttonPlayPause.Content = FontSymbols.Pause;
_context.IsBusy = false;
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{

View File

@@ -397,8 +397,8 @@ public partial class ViewerPanel : UserControl, IDisposable, INotifyPropertyChan
if (audioCodec?.Equals("MIDI", StringComparison.OrdinalIgnoreCase) ?? false)
{
_midiPlayer = new MidiPlayer(this, _context);
_midiPlayer.LoadAndPlay(path, info);
return; // MIDI player will handle the playback at all
_midiPlayer.LoadAndPlay(path);
return; // Midi player will handle the playback at all
}
}