2 回答

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個贊
下面是一個使用 Windows Core Audio Library 的 C# 解決方案
此 API 具有(這與您在卷混合器中看到的內(nèi)容相對應(yīng))的概念。因此,我提供了一個包裝類,它可以為您提供有關(guān)Windows中所有當(dāng)前會話的各種信息,其中包含會話進(jìn)程ID以及可能的名稱,顯示名稱,圖標(biāo)等詳細(xì)信息。SessionAudioSession
此類還具有一個方法,該方法使用 IAudioMeterInformation 接口來獲取每個音頻通道的峰值。GetChannelsPeakValues()
下面是一個 C# 控制臺應(yīng)用示例(但 AudioSession 類支持任何 UI 技術(shù)),該示例在運(yùn)行時將顯示 chrome 瀏覽器實(shí)例的每個通道的峰值(從瀏覽器中運(yùn)行一些視頻或聲音,數(shù)字應(yīng)開始移動)。如果您沒有鑲邊,請使用您選擇的其他流程。
class Program
{
static void Main(string[] args)
{
// Here, I'm just monitoring chrome, one of the possible sessions.
// If you want the whole speakers peak values, use the AudioSession.GetSpeakersChannelsPeakValues() method
foreach (var session in AudioSession.EnumerateAll())
{
if (session.Process?.ProcessName == "chrome")
{
do
{
var values = session.GetChannelsPeakValues();
if (values.Length == 0)
continue;
Console.WriteLine(string.Join(" ", values.Select(v => v.ToString("00%"))));
}
while (true);
}
session.Dispose();
}
}
}
以下是支持 C# 代碼:
public class AudioSession : IDisposable
{
private readonly Lazy<Icon> _icon;
private readonly Lazy<Process> _process;
private readonly IAudioSessionControl2 _control;
public AudioSession(IAudioSessionControl2 control)
{
_control = control;
control.GetState(out var state);
State = state;
control.GetGroupingParam(out var guid);
GroupingParam = guid;
IconPath = GetString(control.GetIconPath);
DisplayName = GetString(control.GetDisplayName);
_icon = new Lazy<Icon>(GetIcon, true);
_process = new Lazy<Process>(() => Process.GetProcessById(ProcessId), true);
Id = GetString(control.GetSessionIdentifier);
InstanceId = GetString(control.GetSessionInstanceIdentifier);
control.GetProcessId(out var pid);
ProcessId = pid;
IsSystemSounds = control.IsSystemSoundsSession() == 0;
}
public AudioSessionState State { get; }
public string IconPath { get; }
public string DisplayName { get; }
public Guid GroupingParam { get; }
public Icon Icon => _icon.Value;
public string Id { get; }
public string InstanceId { get; }
public int ProcessId { get; }
public Process Process => _process.Value;
public bool IsSystemSounds { get; }
public float[] GetChannelsPeakValues()
{
var meter = (IAudioMeterInformation)_control;
meter.GetMeteringChannelCount(out var channelCount);
var values = new float[channelCount];
meter.GetChannelsPeakValues(channelCount, values);
return values;
}
private delegate int GetStringFn(out IntPtr ptr);
private static string GetString(GetStringFn fn)
{
fn(out var ptr);
if (ptr == IntPtr.Zero)
return null;
try
{
var s = Marshal.PtrToStringUni(ptr);
if (!string.IsNullOrWhiteSpace(s) && s.StartsWith("@"))
{
var sb = new StringBuilder(256);
if (SHLoadIndirectString(s, sb, sb.Capacity, IntPtr.Zero) == 0)
{
s = sb.ToString();
}
}
return s;
}
finally
{
Marshal.FreeCoTaskMem(ptr);
}
}
private Icon GetIcon()
{
if (string.IsNullOrWhiteSpace(IconPath))
return null;
var index = ParseIconLocationPath(IconPath, out var path);
// note this may only work if the OS bitness is the same as this process bitness
var hIcon = ExtractIcon(IntPtr.Zero, path, index);
return hIcon == IntPtr.Zero ? null : Icon.FromHandle(hIcon);
}
public override string ToString() => DisplayName;
public void Dispose() => _icon.Value?.Dispose();
public static float[] GetSpeakersChannelsPeakValues()
{
// get the speakers (1st render + multimedia) device
var deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out IMMDevice speakers);
if (speakers == null)
return new float[0];
// get meter information
speakers.Activate(typeof(IAudioMeterInformation).GUID, 0, IntPtr.Zero, out object o);
var meter = (IAudioMeterInformation)o;
if (meter == null)
return new float[0];
meter.GetMeteringChannelCount(out var count);
if (count == 0)
return new float[0];
var values = new float[count];
meter.GetChannelsPeakValues(count, values);
return values;
}
public static IEnumerable<AudioSession> EnumerateAll()
{
// get the speakers (1st render + multimedia) device
var deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out IMMDevice speakers);
if (speakers == null)
yield break;
// activate the session manager, we need the enumerator
speakers.Activate(typeof(IAudioSessionManager2).GUID, 0, IntPtr.Zero, out object o);
var sessionManager = (IAudioSessionManager2)o;
if (sessionManager == null)
yield break;
// enumerate sessions for on this device
sessionManager.GetSessionEnumerator(out IAudioSessionEnumerator sessionEnumerator);
sessionEnumerator.GetCount(out int count);
for (int i = 0; i < count; i++)
{
sessionEnumerator.GetSession(i, out var sessionControl);
if (sessionControl != null)
{
var meter = sessionControl as IAudioMeterInformation;
yield return new AudioSession(sessionControl);
}
}
}
[DllImport("shlwapi", CharSet = CharSet.Unicode)]
private extern static int SHLoadIndirectString(string pszSource, StringBuilder pszOutBuf, int cchOutBuf, IntPtr ppvReserved);
[DllImport("shlwapi", CharSet = CharSet.Unicode)]
private static extern int PathParseIconLocation(string pszIconFile);
[DllImport("shell32", CharSet = CharSet.Unicode)]
private static extern IntPtr ExtractIcon(IntPtr ptr, string pszExeFileName, int nIconIndex);
private static int ParseIconLocationPath(string location, out string path)
{
if (location == null)
throw new ArgumentNullException(nameof(location));
path = string.Copy(location);
int index = PathParseIconLocation(path);
int pos = path.LastIndexOf('\0');
if (pos >= 0)
{
path = path.Substring(0, pos);
}
if (path.StartsWith("@"))
{
path = path.Substring(1);
}
return index;
}
}
[ComImport]
[Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]
public class MMDeviceEnumerator
{
}
public enum EDataFlow
{
eRender,
eCapture,
eAll,
EDataFlow_enum_count
}
public enum ERole
{
eConsole,
eMultimedia,
eCommunications,
ERole_enum_count
}
[Guid("a95664d2-9614-4f35-a746-de8db63617e6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public partial interface IMMDeviceEnumerator
{
[PreserveSig]
int EnumAudioEndpoints(EDataFlow dataFlow, uint dwStateMask, out IntPtr ppDevices);
[PreserveSig]
int GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role, out IMMDevice ppEndpoint);
[PreserveSig]
int GetDevice([MarshalAs(UnmanagedType.LPWStr)] string pwstrId, out IMMDevice ppDevice);
[PreserveSig]
int RegisterEndpointNotificationCallback(IntPtr pClient);
[PreserveSig]
int UnregisterEndpointNotificationCallback(IntPtr pClient);
}
[Guid("d666063f-1587-4e43-81f1-b948e807363f"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public partial interface IMMDevice
{
[PreserveSig]
int Activate([MarshalAs(UnmanagedType.LPStruct)] Guid iid, uint dwClsCtx, [In, Out] IntPtr pActivationParams, [MarshalAs(UnmanagedType.IUnknown)] out object ppInterface);
[PreserveSig]
int OpenPropertyStore(uint stgmAccess, out IntPtr ppProperties);
[PreserveSig]
int GetId(out IntPtr ppstrId);
[PreserveSig]
int GetState(out uint pdwState);
}
[Guid("C02216F6-8C67-4B5B-9D00-D008E73E0064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IAudioMeterInformation
{
[PreserveSig]
int GetPeakValue(out float pfPeak);
[PreserveSig]
int GetMeteringChannelCount(out int pnChannelCount);
[PreserveSig]
int GetChannelsPeakValues(int u32ChannelCount, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] float[] afPeakValues);
[PreserveSig]
int QueryHardwareSupport(out int pdwHardwareSupportMask);
}
[Guid("77aa99a0-1bd6-484f-8bc7-2c654c9a9b6f"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public partial interface IAudioSessionManager2
{
// IAudioSessionManager
[PreserveSig]
int GetAudioSessionControl(IntPtr AudioSessionGuid, uint StreamFlags, out IAudioSessionControl2 SessionControl);
[PreserveSig]
int GetSimpleAudioVolume(IntPtr AudioSessionGuid, uint StreamFlags, out IntPtr AudioVolume);
// IAudioSessionManager2
[PreserveSig]
int GetSessionEnumerator(out IAudioSessionEnumerator SessionEnum);
[PreserveSig]
int RegisterSessionNotification(IntPtr SessionNotification);
[PreserveSig]
int UnregisterSessionNotification(IntPtr SessionNotification);
[PreserveSig]
int RegisterDuckNotification([MarshalAs(UnmanagedType.LPWStr)] string sessionID, IntPtr duckNotification);
[PreserveSig]
int UnregisterDuckNotification(IntPtr duckNotification);
}
[Guid("E2F5BB11-0570-40CA-ACDD-3AA01277DEE8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IAudioSessionEnumerator
{
[PreserveSig]
int GetCount(out int SessionCount);
[PreserveSig]
int GetSession(int SessionCount, out IAudioSessionControl2 Session);
}
public enum AudioSessionState
{
AudioSessionStateInactive = 0,
AudioSessionStateActive = 1,
AudioSessionStateExpired = 2,
}
[Guid("bfb7ff88-7239-4fc9-8fa2-07c950be9c6d"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public partial interface IAudioSessionControl2
{
// IAudioSessionControl
[PreserveSig]
int GetState(out AudioSessionState pRetVal);
[PreserveSig]
int GetDisplayName(out IntPtr pRetVal);
[PreserveSig]
int SetDisplayName([MarshalAs(UnmanagedType.LPWStr)] string Value, [MarshalAs(UnmanagedType.LPStruct)] Guid EventContext);
[PreserveSig]
int GetIconPath(out IntPtr pRetVal);
[PreserveSig]
int SetIconPath([MarshalAs(UnmanagedType.LPWStr)] string Value, [MarshalAs(UnmanagedType.LPStruct)] Guid EventContext);
[PreserveSig]
int GetGroupingParam(out Guid pRetVal);
[PreserveSig]
int SetGroupingParam([MarshalAs(UnmanagedType.LPStruct)] Guid Override, [MarshalAs(UnmanagedType.LPStruct)] Guid EventContext);
[PreserveSig]
int RegisterAudioSessionNotification(IntPtr NewNotifications);
[PreserveSig]
int UnregisterAudioSessionNotification(IntPtr NewNotifications);
// IAudioSessionControl2
[PreserveSig]
int GetSessionIdentifier(out IntPtr pRetVal);
[PreserveSig]
int GetSessionInstanceIdentifier(out IntPtr pRetVal);
[PreserveSig]
int GetProcessId(out int pRetVal);
[PreserveSig]
int IsSystemSoundsSession();
[PreserveSig]
int SetDuckingPreference(bool optOut);
}

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超7個贊
這個(我的解決方案)工作的關(guān)鍵是,當(dāng)這個表單運(yùn)行時,你會讓你的音量混合器顯示。
這可能看起來有點(diǎn)外在,但它可能有效,并且比嘗試?yán)寐暱ㄐ畔⒁菀椎枚?。我的建議是使用顏色識別和一些好的ol win32 api。首先,您需要顯示您的音量混音器(或聲音設(shè)置),就像我在這里所做的那樣(我將在本例中使用音量混音器,并專注于Amazon Music控件):
然后,使用以下代碼(模塊和窗體):
Imports System.Runtime.InteropServices
Module Module1
<DllImport("user32.dll")>
Private Function GetDC(ByVal hwnd As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")>
Private Function ReleaseDC(ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As Int32
End Function
<DllImport("gdi32.dll")>
Private Function GetPixel(ByVal hdc As IntPtr, ByVal nXPos As Integer, ByVal nYPos As Integer) As UInteger
End Function
Public Function GetPixelColor(ByVal x As Integer, ByVal y As Integer) As System.Drawing.Color
Dim hdc As IntPtr = GetDC(IntPtr.Zero)
Dim pixel As UInteger = GetPixel(hdc, x, y)
Dim clr As Color
ReleaseDC(IntPtr.Zero, hdc)
clr = Color.FromArgb(255, (pixel And &HFF), (pixel And &HFF00) >> 8,
(pixel And &HFF0000) >> 16)
Return clr
End Function
End Module
這是表單代碼(簡單):
Public Class Form1
Const c_blnDebug as Boolean = True
'Make false to run your program with settings
'Make true to get location and colors
Const c_intRedThresh As Integer = 90
'Threshold color must be less than or equal to this
Const c_intGreenThresh As Integer = 170
'Threshold color must be greater than or equal to this
Const c_intBlueThresh As Integer = 90
'Threshold color must be less than or equal to this
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim ptArr() As Point
Dim intI As Integer
Dim clrTemp As Color
Dim intRed As Integer
Dim intGreen As Integer
Dim intBlue As Integer
'Set the pixel locations to watch if NOT DEBUGGING
ReDim ptArr(0 To 2)
'at source level
ptArr(0).X = 1762
ptArr(0).Y = 870
'-1 pixel
ptArr(1).X = 1762
ptArr(1).Y = 869
'+1 pixel
ptArr(2).X = 1762
ptArr(2).Y = 871
If c_blnDebug Then
Debug.Print(GetPixelColor(MousePosition.X, MousePosition.Y).ToString & vbCrLf &
"X: " & MousePosition.X & vbCrLf & "Y: " & MousePosition.Y)
Else
For intI = 0 To 2
clrTemp = GetPixelColor(ptArr(intI).X, ptArr(intI).Y)
intRed = clrTemp.R
intGreen = clrTemp.G
intBlue = clrTemp.B
If ((intRed < c_intRedThresh) And
(intGreen >= c_intGreenThresh) And
(intBlue <= c_intBlueThresh)) Then
'Sound Spike do your business
Debug.Print("Found Spike @ " & Now)
Exit For
End If
Next intI
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If c_blnDebug Then
Timer1.Interval = 1000 '1 second
Else
Timer1.Interval = 250 '0.25 seconds
End If
Timer1.Enabled = True
End Sub
End Class
然后可以為您提供需要觀看的鼠標(biāo)位置以及顏色(大致如圖1000.print所示),如我的debug.print結(jié)果所示:
Color [A=255, R=51, G=190, B=51]
X: 1762
Y: 870
Color [A=255, R=51, G=190, B=51]
X: 1762
Y: 870
Color [A=255, R=51, G=191, B=51]
X: 1762
Y: 870
Color [A=255, R=51, G=188, B=51]
X: 1762
Y: 870
Color [A=255, R=51, G=195, B=51]
X: 1762
Y: 870
Color [A=255, R=232, G=17, B=35]
X: 1491
Y: 646
因此,我選擇的是觀察鼠標(biāo)位置坐標(biāo)+/- (1)像素。對于 (紅色 < 90)、(綠色> 170) 和 (藍(lán)色 < 90) 作為閾值。
完成此操作后,我可以簡單地將計(jì)時器設(shè)置為每250毫秒一次并檢查顏色。這就像一個魅力,超級簡單!
我希望這有幫助,我得到以下結(jié)果!
Found Spike @ 2/5/2019 10:16:14 AM
Found Spike @ 2/5/2019 10:16:17 AM
Found Spike @ 2/5/2019 10:16:17 AM
Found Spike @ 2/5/2019 10:16:18 AM
Found Spike @ 2/5/2019 10:16:19 AM
Found Spike @ 2/5/2019 10:16:19 AM
Found Spike @ 2/5/2019 10:16:21 AM
Found Spike @ 2/5/2019 10:16:21 AM
Found Spike @ 2/5/2019 10:16:21 AM
Found Spike @ 2/5/2019 10:16:21 AM
Found Spike @ 2/5/2019 10:16:23 AM
而且,這是最終產(chǎn)品的屏幕截圖(上面的代碼沒有反映這一點(diǎn),只是簡單的方法)(或者至少在我決定添加更多之前):
- 2 回答
- 0 關(guān)注
- 160 瀏覽
添加回答
舉報(bào)