Fix MIDI player ShouldLoop not togglable

This commit is contained in:
ema
2024-12-28 02:03:18 +08:00
parent a1cd8a1e3c
commit 4038cf2bb5

View File

@@ -19,7 +19,6 @@ using Melanchall.DryWetMidi.Core;
using Melanchall.DryWetMidi.Interaction; using Melanchall.DryWetMidi.Interaction;
using Melanchall.DryWetMidi.Multimedia; using Melanchall.DryWetMidi.Multimedia;
using QuickLook.Common.Annotations; using QuickLook.Common.Annotations;
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin; using QuickLook.Common.Plugin;
using System; using System;
using System.ComponentModel; using System.ComponentModel;
@@ -43,7 +42,6 @@ internal class MidiPlayer : IDisposable, INotifyPropertyChanged
private OutputDevice _outputDevice; private OutputDevice _outputDevice;
private Playback _playback; private Playback _playback;
private TimeSpan _duration; private TimeSpan _duration;
private MethodInfo _setShouldLoop; // Reflection to invoke `_vp.set_ShouldLoop()`
private long _currentTicks = 0L; private long _currentTicks = 0L;
@@ -64,6 +62,7 @@ internal class MidiPlayer : IDisposable, INotifyPropertyChanged
{ {
_vp = panle; _vp = panle;
_context = context; _context = context;
_outputDevice = OutputDevice.GetByName("Microsoft GS Wavetable Synth");
} }
public void Dispose() public void Dispose()
@@ -84,7 +83,6 @@ internal class MidiPlayer : IDisposable, INotifyPropertyChanged
_vp.metaArtists.Text = _midiFile.OriginalFormat.ToString(); _vp.metaArtists.Text = _midiFile.OriginalFormat.ToString();
_vp.metaAlbum.Text = _midiFile.TimeDivision.ToString(); _vp.metaAlbum.Text = _midiFile.TimeDivision.ToString();
_outputDevice = OutputDevice.GetByName("Microsoft GS Wavetable Synth");
_playback = _midiFile.GetPlayback(_outputDevice); _playback = _midiFile.GetPlayback(_outputDevice);
if (_playback.GetDuration(TimeSpanType.Metric) is MetricTimeSpan metricTimeSpan) if (_playback.GetDuration(TimeSpanType.Metric) is MetricTimeSpan metricTimeSpan)
@@ -123,15 +121,8 @@ internal class MidiPlayer : IDisposable, INotifyPropertyChanged
} }
}; };
if (_vp.GetType().GetProperty("ShouldLoop", BindingFlags.Instance | BindingFlags.Public) is PropertyInfo propertyShouldLoop)
{
_setShouldLoop = propertyShouldLoop.GetSetMethod(nonPublic: true);
_setShouldLoop.Invoke(_vp, [SettingHelper.Get("ShouldLoop", false, "QuickLook.Plugin.VideoViewer")]);
}
_vp.buttonLoop.Click += (_, _) => _vp.buttonLoop.Click += (_, _) =>
{ {
_setShouldLoop.Invoke(_vp, [!_vp.ShouldLoop]);
_playback.Loop = _vp.ShouldLoop; _playback.Loop = _vp.ShouldLoop;
}; };