Reputation: 8721
I'm trying to grab media info from my personal fave Media Player Classic using a C# app.
Suppose I got the window handle of the MPC-HC instance I'm interested in, but WM_GETTEXT
only gets me the window title. I'm not satisfied with just that. I want to also grab play state (stopped/paused/playing), current time, total length and file path. I should be able to get all other things from the file, knowing its path.
First thing I thought was using AutoIt Window Info app, which gets me just the stuff I want in its Visible Text tab, and I'm okay with using it as a tool to get the text, but how am I gonna get that text directly to my C# app for parsing?
I'd like to find a solution that doesn't involve interfering with user's activities, like forcibly bringing up front the player's window. I just want to get the current time, play state and full file path into my C# app. Is there an easy way to do that?
Upvotes: 4
Views: 4670
Reputation: 8721
I found out that by enabling MPC-HC web interface, a nice page appears with all the current player's stats I need at this address: http://localhost:13579/variables.html
(13579 is the default port, but you may change it in the options). Check Allow access from localhost only to protect your secret musical desires ;)
The HTML on that page is kinda like this:
<html>
<head>
<title>MPC-HC WebServer - Variables</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="default.css">
</head>
<body>
<p id="filepatharg">C:\music.mp3</p>
<p id="filepath">C:\music.mp3</p>
<p id="filedirarg">C:\</p>
<p id="filedir">C:\</p>
<p id="state">1</p>
<p id="statestring">Paused</p>
<p id="position">85918</p>
<p id="positionstring">00:01:25</p>
<p id="duration">284525</p>
<p id="durationstring">00:04:44</p>
<p id="volumelevel">50</p>
<p id="muted">0</p>
<p id="playbackrate">1</p>
<p id="reloadtime">0</p>
</body>
Upvotes: 9
Reputation: 7385
You can get required information using information retrieved from child windows, look at EnumChildWindows
function, here is code snippet which demonstrate such behaviour:
class Program
{
public delegate bool WindowEnumDelegate(IntPtr hwnd,
int lParam);
[DllImport("user32.dll")]
public static extern int EnumChildWindows(IntPtr hwnd,
WindowEnumDelegate del,
int lParam);
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hwnd,
StringBuilder bld, int size);
static void Main(string[] args)
{
var mainWindowHandle = Process.GetProcessesByName("mpc-hc").First().MainWindowHandle;
var list = new List<string>();
EnumChildWindows(mainWindowHandle, (hwnd, param) =>
{
var bld = new StringBuilder(256);
GetWindowText(hwnd, bld, 256);
var text = bld.ToString();
if (!string.IsNullOrEmpty(text))
list.Add(text);
return true;
}, 0);
Console.WriteLine("length={0}", list[0]);
Console.WriteLine("state={0}", list[1]);
Console.WriteLine("bitrate={0}", list[5]);
Console.WriteLine("name={0}", list[7]);
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
}
You can also explore additional subwindows using spy++ like this:
Upvotes: 2