Inital commit
This commit is contained in:
116
src/SoundBrowser/MainWindow.xaml.cs
Normal file
116
src/SoundBrowser/MainWindow.xaml.cs
Normal file
@@ -0,0 +1,116 @@
|
||||
using CoreAudio;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace SoundBrowser
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
dele = new WinEventDelegate(WinEventProc);
|
||||
IntPtr m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT);
|
||||
}
|
||||
|
||||
|
||||
WinEventDelegate dele = null;
|
||||
|
||||
|
||||
delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
|
||||
|
||||
private const uint WINEVENT_OUTOFCONTEXT = 0;
|
||||
private const uint EVENT_SYSTEM_FOREGROUND = 3;
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
static extern IntPtr GetForegroundWindow();
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int processId);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
|
||||
|
||||
private static SimpleAudioVolume GetVolumeObject(Process process)
|
||||
{
|
||||
var deviceEnumerator = new MMDeviceEnumerator(Guid.NewGuid());
|
||||
|
||||
using var device = deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
|
||||
using var manager = device.AudioSessionManager2;
|
||||
|
||||
var sessions = manager.Sessions;
|
||||
|
||||
foreach (var session in sessions)
|
||||
{
|
||||
if (session.ProcessID == process.Id)
|
||||
{
|
||||
return session.SimpleAudioVolume;
|
||||
}
|
||||
|
||||
var audioProcess = Process.GetProcessById((int)session.ProcessID);
|
||||
if(audioProcess?.ProcessName == process.ProcessName)
|
||||
{
|
||||
Console.WriteLine(process.MainModule.FileVersionInfo.FileDescription);
|
||||
return session.SimpleAudioVolume;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
SimpleAudioVolume _current;
|
||||
|
||||
private string GetActiveWindowTitle()
|
||||
{
|
||||
const int nChars = 256;
|
||||
IntPtr handle = IntPtr.Zero;
|
||||
StringBuilder Buff = new StringBuilder(nChars);
|
||||
handle = GetForegroundWindow();
|
||||
|
||||
if (handle <= 0)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
var tid = GetWindowThreadProcessId(handle, out var pid);
|
||||
var process = Process.GetProcessById(pid);
|
||||
|
||||
var vol = GetVolumeObject(process);
|
||||
_current = vol;
|
||||
if(vol != null)
|
||||
{
|
||||
vol.Mute = true;
|
||||
}
|
||||
|
||||
return $"{pid} vol:{vol?.MasterVolume ?? -1} - {process.ProcessName}";
|
||||
}
|
||||
|
||||
|
||||
public void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
|
||||
{
|
||||
_tf.Text = GetActiveWindowTitle();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user