我想了解所有当前活跃的媒体回放的信息。我找到了一种方法来读出所有的媒体会话here
using Windows.Media.Control;
namespace ConsoleApp1 {
public class Program {
[STAThread]
static void Main(string[] args) {
var gsmtcsm = GlobalSystemMediaTransportControlsSessionManager.RequestAsync().GetAwaiter().GetResult().GetSessions();
foreach (var session in gsmtcsm) {
var mediaProperties = session.TryGetMediaPropertiesAsync().GetAwaiter().GetResult();
Console.WriteLine("{0} - {1}", mediaProperties.Artist, mediaProperties.Title);
}
Console.ReadKey();
}
}
}现在我想得到这些会议的相应程序。另外,如果程序存在,我希望得到它的窗口。我的目标是以编程方式将窗口移动到另一个屏幕上。程序句柄仅用作标识符。
例如:我打开一个随机的.mp4文件。默认情况下,它由Windows影视公司播放。现在我想要获得会话、程序和窗口(影视有一个窗口),并将其移动到另一个屏幕(通过代码)
另一个例子:我在Youtube上看了一段视频。现在我想要打开Youtube浏览器的窗口。
发布于 2020-11-08 01:48:43
您可以找到运行媒体会话的应用程序的主窗口。
var gsmtcsm = GlobalSystemMediaTransportControlsSessionManager.RequestAsync().GetAwaiter().GetResult().GetSessions();
foreach (var session in gsmtcsm)
{
string modelId = session.SourceAppUserModelId;
// Get all processes
Process[] processlist = Process.GetProcesses();
// Create and array to hold matched processes
List<Process> modelProcesslist = new List<Process>();
// Using a direct filter on the Process.GetProcesses() call will raise an exception, you will need to cycle through them
// to find a process that has the same filename as reported by the media session
// The filename may be different to the process name
foreach (Process p in processlist)
{
try
{
if (p.MainModule.FileName.Contains(modelId) && p.MainWindowHandle != IntPtr.Zero)
{
modelProcesslist.Add(p);
}
}
catch(System.Exception)
{
// Couldn't look at the MainModule of this process, move on
}
}
foreach (Process p in modelProcesslist)
{
IntPtr windowHandle = p.MainWindowHandle;
// The main window(s) for apps that have the same name as the source app for the media session
}
}一个大问题是,如果主窗口属于chrome.exe (或任何选项卡式浏览器),那么媒体播放器就在其中一个子选项卡中,在浏览器的一个选项卡中查找内容是一个全新的难题。
另外,如果您打开了多个浏览器窗口,它们都将具有相同的可执行文件名,并被上面的代码获取。
如果应用程序是一个专用的媒体播放器,你应该能够使用手柄移动窗口。
https://stackoverflow.com/questions/64733464
复制相似问题