Skip to content

Commit

Permalink
Fix crash when on versions older than Windows 10
Browse files Browse the repository at this point in the history
Fixes #8
  • Loading branch information
sabihoshi committed May 8, 2021
1 parent b93f337 commit 97dd880
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
2 changes: 1 addition & 1 deletion GenshinLyreMidiPlayer.WPF/GenshinLyreMidiPlayer.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<UseWPF>true</UseWPF>
<StartupObject>GenshinLyreMidiPlayer.WPF.App</StartupObject>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Version>1.9.2</Version>
<Version>1.9.3</Version>
<ApplicationIcon>item_windsong_lyre.ico</ApplicationIcon>
<Nullable>enable</Nullable>
<RepositoryUrl>https://github.com/sabihoshi/GenshinLyreMidiPlayer</RepositoryUrl>
Expand Down
61 changes: 35 additions & 26 deletions GenshinLyreMidiPlayer.WPF/ViewModels/LyrePlayerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class LyrePlayerViewModel : Screen,
{
private static readonly Settings Settings = Settings.Default;
private readonly IEventAggregator _events;
private readonly MediaPlayer _player;
private readonly MediaPlayer? _player;
private readonly SettingsPageViewModel _settings;
private readonly OutputDevice _speakers;
private readonly PlaybackCurrentTimeWatcher _timeWatcher;
Expand All @@ -50,13 +50,19 @@ public LyrePlayerViewModel(IContainer ioc,

_timeWatcher.CurrentTimeChanged += OnSongTick;

_player = ioc.Get<MediaPlayer>();
// SystemMediaTransportControls is only supported on Windows 10 and later
// https://docs.microsoft.com/en-us/uwp/api/windows.media.systemmediatransportcontrols
if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
Environment.OSVersion.Version.Major >= 10)
{
_player = ioc.Get<MediaPlayer>();

_player.CommandManager.NextReceived += (_, _) => Next();
_player.CommandManager.PreviousReceived += (_, _) => Previous();
_player.CommandManager.NextReceived += (_, _) => Next();
_player.CommandManager.PreviousReceived += (_, _) => Previous();

_player.CommandManager.PlayReceived += (_, _) => PlayPause();
_player.CommandManager.PauseReceived += (_, _) => PlayPause();
_player.CommandManager.PlayReceived += (_, _) => PlayPause();
_player.CommandManager.PauseReceived += (_, _) => PlayPause();
}
}

public BindableCollection<MidiInput> MidiInputs { get; } = new()
Expand Down Expand Up @@ -127,7 +133,8 @@ public MidiInput? SelectedMidiInput
}
}

private MusicDisplayProperties Display => _player.SystemMediaTransportControls.DisplayUpdater.MusicProperties;
private MusicDisplayProperties? Display =>
_player?.SystemMediaTransportControls.DisplayUpdater.MusicProperties;

public Playback? Playback { get; private set; }

Expand All @@ -139,7 +146,8 @@ public MidiInput? SelectedMidiInput

public string PlayPauseIcon => Playback?.IsRunning ?? false ? PauseIcon : PlayIcon;

private SystemMediaTransportControls Controls => _player.SystemMediaTransportControls;
private SystemMediaTransportControls? Controls =>
_player?.SystemMediaTransportControls;

public TimeSpan CurrentTime => TimeSpan.FromSeconds(SongSlider);

Expand Down Expand Up @@ -176,30 +184,31 @@ public void UpdateButtons()
NotifyOfPropertyChange(() => PlayPauseIcon);
NotifyOfPropertyChange(() => MaximumTime);

var controls = Controls;
if (Controls is not null && Display is not null)
{
Controls.IsPlayEnabled = CanHitPlayPause;
Controls.IsPauseEnabled = CanHitPlayPause;

controls.IsPlayEnabled = CanHitPlayPause;
controls.IsPauseEnabled = CanHitPlayPause;
Controls.IsNextEnabled = CanHitNext;
Controls.IsPreviousEnabled = CanHitPrevious;

controls.IsNextEnabled = CanHitNext;
controls.IsPreviousEnabled = CanHitPrevious;
Controls.PlaybackStatus =
Playlist.OpenedFile is null ? MediaPlaybackStatus.Closed :
Playback is null ? MediaPlaybackStatus.Stopped :
Playback.IsRunning ? MediaPlaybackStatus.Playing :
MediaPlaybackStatus.Paused;

controls.PlaybackStatus =
Playlist.OpenedFile is null ? MediaPlaybackStatus.Closed :
Playback is null ? MediaPlaybackStatus.Stopped :
Playback.IsRunning ? MediaPlaybackStatus.Playing :
MediaPlaybackStatus.Paused;
var file = Playlist.OpenedFile;
if (file is not null)
{
var position = $"{file.Position}/{Playlist.GetPlaylist().Count}";

var file = Playlist.OpenedFile;
if (file is not null)
{
var position = $"{file.Position}/{Playlist.GetPlaylist().Count}";
Display.Title = file.Title;
Display.Artist = $"Playing {position} {CurrentTime:mm\\:ss}";
}

Display.Title = file.Title;
Display.Artist = $"Playing {position} {CurrentTime:mm\\:ss}";
Controls.DisplayUpdater.Update();
}

controls.DisplayUpdater.Update();
}

private void InitializeTracks()
Expand Down
10 changes: 5 additions & 5 deletions GenshinLyreMidiPlayer.WPF/app.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@
and Windows will automatically select the most compatible environment. -->

<!-- Windows Vista -->
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />

<!-- Windows 7 -->
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />

<!-- Windows 8 -->
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />

<!-- Windows 8.1 -->
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />

<!-- Windows 10 -->
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />

</application>
</compatibility>
Expand Down

0 comments on commit 97dd880

Please sign in to comment.