Skip to content

Commit

Permalink
pause video when screen locked #375
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Dec 3, 2023
1 parent abb4ec0 commit af116cc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/Starward/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Starward.Pages.Welcome;
using System;
using System.IO;
using System.Runtime.InteropServices;
using Vanara.PInvoke;
using Windows.Graphics;
using Windows.UI;
Expand Down Expand Up @@ -64,6 +65,8 @@ private void InitializeWindowSubclass()
WTSRegisterSessionNotification(MainWindow.Current.HWND, 0);
}



private void InitializeMainWindow(string? action = null)
{
HWND = WindowNative.GetWindowHandle(this);
Expand Down Expand Up @@ -298,8 +301,25 @@ public void ChangeAccentColor(Color? backColor = null, Color? foreColor = null)



[DllImport("Wtsapi32.dll")]
private static extern bool WTSRegisterSessionNotification(IntPtr hWnd, int dwFlags);


public IntPtr SUBCLASSPROC(HWND hWnd, uint uMsg, IntPtr wParam, IntPtr lParam, nuint uIdSubclass, IntPtr dwRefData)
{
if (uMsg == ((uint)User32.WindowMessage.WM_WTSSESSION_CHANGE))
{
// WTS_SESSION_LOCK
if (wParam == 0x7)
{
MainPage.Current?.PauseVideo(true);
}
// WTS_SESSION_UNLOCK
if (wParam == 0x8)
{
MainPage.Current?.PlayVideo(true);
}
}
if (uMsg == ((uint)User32.WindowMessage.WM_SYSCOMMAND))
{
// SC_MAXIMIZE
Expand Down
21 changes: 17 additions & 4 deletions src/Starward/Pages/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ partial void OnVideoBgVolumeChanged(int value)

public bool IsPlayingVideo { get; private set; }



private void InitializeBackgroundImage()
{
Expand Down Expand Up @@ -699,15 +699,28 @@ private void MediaPlayer_VideoFrameAvailable(MediaPlayer sender, object args)
}


private bool pausedBySessionLocked;


public void PlayVideo()
public void PlayVideo(bool sessionUnlock = false)
{
mediaPlayer?.Play();
if (!sessionUnlock)
{
mediaPlayer?.Play();
}
if (sessionUnlock && pausedBySessionLocked)
{
mediaPlayer?.Play();
}
}


public void PauseVideo()
public void PauseVideo(bool sessionLock = false)
{
if (mediaPlayer?.PlaybackSession?.PlaybackState is MediaPlaybackState.Playing)
{
pausedBySessionLocked = sessionLock;
}
mediaPlayer?.Pause();
}

Expand Down

0 comments on commit af116cc

Please sign in to comment.