Last active
June 9, 2024 14:01
-
-
Save KOZ60/723c049b38be5cee92754fdc7c7b4390 to your computer and use it in GitHub Desktop.
現在演奏中のメディアファイルを列挙する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#nullable disable | |
#pragma warning disable | |
using System.Diagnostics; | |
using System.Net.NetworkInformation; | |
using System.Reflection; | |
using System.Security.Principal; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
using NAudio.CoreAudioApi; | |
// | |
// NAudio を nuget から、handle.exe(DBCS対応版)を下の URL から入手してください。 | |
// https://github.com/PolarGoose/Sysinternals-console-utils-with-Unicode-support/releases | |
// | |
static class Program | |
{ | |
// result: | |
// vlc C:\Temp\A.mp3 | |
// wmplayer C:\Temp\B.mp3 | |
// Microsoft.Media.Player C:\Temp\日本語.mp3 | |
[STAThread] | |
static void Main() { | |
// If the program is not started as an administrator, grant it and start it. | |
// You can also set the following in the application manifest. | |
// <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> | |
if (!IsAdministratorRights) { | |
StartAdministratorRights(); | |
return; | |
} | |
// Enumerate devices | |
var deviceEnumerator = new MMDeviceEnumerator(); | |
foreach (var device in deviceEnumerator.EnumerateAudioEndPoints( | |
DataFlow.Render, DeviceState.Active)) { | |
// Sessions with the device open | |
// SessionCollection is not implements IEnumurable :( | |
var sessionManager = device.AudioSessionManager; | |
var sessions = sessionManager.Sessions; | |
for (int i = 0; i < sessions.Count; i++) { | |
var session = sessions[i]; | |
var volume = session.AudioMeterInformation.MasterPeakValue; | |
if (volume > 0) { | |
using (var process = Process.GetProcessById((int)session.GetProcessID)) { | |
var fileName = GetUseMultimediaFile(process); | |
if (fileName != null) { | |
Console.WriteLine($"{process.ProcessName.PadRight(26)} {fileName}"); | |
} | |
} | |
} | |
} | |
} | |
} | |
private static bool IsAdministratorRights { | |
get { | |
using (var identity = WindowsIdentity.GetCurrent()) { | |
var principal = new WindowsPrincipal(identity); | |
return principal.IsInRole(WindowsBuiltInRole.Administrator); | |
} | |
} | |
} | |
private static void StartAdministratorRights() { | |
var myProgramName = Path.ChangeExtension(Assembly.GetEntryAssembly().Location, ".exe"); | |
var tempFileName = Path.GetTempFileName(); | |
ProcessStartInfo info = new ProcessStartInfo(); | |
info.FileName = "cmd.exe"; | |
info.Arguments = $"/C \"\"\"{myProgramName}\"\" > {tempFileName}\""; | |
info.CreateNoWindow = false; | |
info.UseShellExecute = true; | |
info.Verb = "RunAs"; | |
using (var process = Process.Start(info)) { | |
process.WaitForExit(); | |
Console.WriteLine(File.ReadAllText(tempFileName)); | |
File.Delete(tempFileName); | |
} | |
} | |
private static string[] MultimediaExtensions = [ | |
"mp3","wav","flac","aac","ogg","m4a","wma","alac", | |
"aiff","opus","pcm","dsd","ape","mpc","wv", | |
"avi","mp4","mkv","flv","mov","wmv","webm","mpeg","mpg", | |
"m4v","3gp","vob","ogv","rm","rmvb","mts","m2ts","ts", | |
]; | |
private static string MultimediaPattern { | |
get { | |
var sb = new StringBuilder(); | |
foreach (var s in MultimediaExtensions) { | |
sb.Append($"|\\.{s}"); | |
} | |
return sb.ToString(2, sb.Length - 2); | |
} | |
} | |
// If non-English characters are garbled, | |
// download the unicode version from | |
// https://github.com/PolarGoose/Sysinternals-console-utils-with-Unicode-support/releases | |
// However, it appears that it is not Unicode compatible, but only DBCS compatible. | |
private static string GetUseMultimediaFile(Process process) { | |
var psi = new ProcessStartInfo(); | |
psi.UseShellExecute = false; | |
psi.FileName = "cmd.exe"; | |
psi.RedirectStandardInput = true; | |
psi.RedirectStandardOutput = true; | |
psi.StandardInputEncoding = Encoding.Default; | |
psi.StandardOutputEncoding = Encoding.Default; | |
using (var p = Process.Start(psi)) { | |
using (var writer = p.StandardInput) { | |
writer.WriteLine($"handle64_v5.0_Unicode.exe -vt -p {process.Id}"); | |
} | |
using (var reader = p.StandardOutput) { | |
var alines = reader.ReadToEnd(); | |
p.WaitForExit(); | |
var lines = alines.Split("\r\n"); | |
var line = lines.Where(line => Regex.IsMatch(line, | |
MultimediaPattern, | |
RegexOptions.IgnoreCase)) | |
.FirstOrDefault(); | |
if (line != null) { | |
return line.Split("\t")[6]; | |
} | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment