简体   繁体   English

如何在 C# 中与 Windows Media Player 交互

[英]How to interact with Windows Media Player in C#

I am looking for a way to interact with a standalone full version of Windows Media Player.我正在寻找一种与独立完整版 Windows Media Player 进行交互的方法。
Mostly I need to know the Path of the currently played track.大多数情况下,我需要知道当前播放曲目的路径。

The iTunes SDK makes this really easy but unfortunately there really isn't any way to do it with Windows Media Player, at least not in .Net(C#) without any heavy use of pinvoke, which I am not really comfortable with. iTunes SDK 使这变得非常简单,但不幸的是,Windows Media Player 确实没有任何方法可以做到这一点,至少在 .Net(C#) 中没有任何大量使用 pinvoke 的方法,我对此并不满意。

Thanks谢谢

Just to clearify: I don't want to embedded a new instance of Windows Media Player in my app, but instead control/read the "real" full version of Windows Media Player, started seperatly by the user只是为了澄清:我不想在我的应用程序中嵌入 Windows Media Player 的新实例,而是控制/读取由用户单独启动的“真实”完整版 Windows Media Player

Just add a reference to wmp.dll (\\windows\\system32\\wmp.dll)只需添加对 wmp.dll (\\windows\\system32\\wmp.dll) 的引用

using WMPLib;

And then you can instantiate a media player然后你可以实例化一个媒体播放器

var Player = new WindowsMediaPlayer();
// Load a playlist or file and then get the title 
var title = Player.controls.currentItem.name;

See Creating the Windows Media Player Control Programmatically for more information有关详细信息,请参阅以编程方式创建 Windows Media Player 控件

For remoting the Windows Media Player, you can use the IWMPRemoteMediaServices interface to control the stand alone Windows Media Player.对于远程处理 Windows Media Player,您可以使用 IWMPRemoteMediaServices 接口来控制独立的 Windows Media Player。 And you should be able to read all the informations you want like title or filename from your WMP player object.并且您应该能够从 WMP 播放器对象中读取您想要的所有信息,例如标题或文件名。 Unfortunately there is no C# smaple code in the SDK included.不幸的是,SDK 中没有包含 C# smaple 代码。 You can get the files from here: http://d.hatena.ne.jp/punidama/20080227 Look for the file WmpRemote.zip Originally it's from here: http://blogs.msdn.com/ericgu/archive/2005/06/22/431783.aspx您可以从这里获取文件: http ://d.hatena.ne.jp/punidama/20080227 查找文件 WmpRemote.zip 最初来自这里: http : //blogs.msdn.com/ericgu/archive/2005 /06/22/431783.aspx

Then you have to cast to the WindowsMediaPlayer object: RemotedWindowsMediaPlayer rm = new RemotedWindowsMediaPlayer();然后你必须转换到 WindowsMediaPlayer 对象: RemotedWindowsMediaPlayer rm = new RemotedWindowsMediaPlayer(); WMPLib.WindowsMediaPlayer myPlayer = this.GetOcx() as WMPLib.WindowsMediaPlayer; WMPLib.WindowsMediaPlayer myPlayer = this.GetOcx() as WMPLib.WindowsMediaPlayer;

and there you go..你去吧..

I had this https://social.msdn.microsoft.com/Forums/vstudio/en-US/dbd43d7e-f3a6-4087-be06-df17e76b635d/windows-media-player-remoting-in-c?forum=clr in my bookmarks but have NOT tested it in anyway.我有这个https://social.msdn.microsoft.com/Forums/vstudio/en-US/dbd43d7e-f3a6-4087-be06-df17e76b635d/windows-media-player-remoting-in-c?forum=clr在我的书签,但无论如何都没有对其进行测试。 Just a pointer in the right direction.只是一个指向正确方向的指针。 It's nothing official and will require a bit of digging, but you should get a fairly simple wrapper (which will still use PInvoke under the hood - but you won't see it) around Windows Media Player.这不是官方的,需要一些挖掘,但是您应该在 Windows Media Player 周围获得一个相当简单的包装器(它仍然会在引擎盖下使用 PInvoke - 但您不会看到它)。

Hope that helps.希望有帮助。

Oh, I misunderstood.哦,我误会了。 I thought you were talking about controlling the currently running Windows Media Player instance.我以为您在谈论控制当前运行的 Windows Media Player 实例。 If you are hosting Windows Media Player yourself then WMPLib is certainly the better solution.如果您自己托管 Windows Media Player,那么 WMPLib 无疑是更好的解决方案。

The best info I have seen on interacting with Windows Media Player is this article written by Stephen Toub.我所看到的有关与 Windows Media Player 交互的最佳信息是 Stephen Toub 撰写的这篇文章

He lists a whole load of different ways to play dvr-ms files (doesn't really matter what format they are for this though).他列出了大量不同的播放 dvr-ms 文件的方法(尽管它们的格式并不重要)。 The bit that is possibly of interest to you is about using a Media Player ActiveX Control, which you can add to the visual Studio toolbox by right-clicking and adding the Windows Media Player ActiveX COM Control.您可能感兴趣的一点是关于使用 Media Player ActiveX 控件,您可以通过右键单击并添加 Windows Media Player ActiveX COM 控件将其添加到 Visual Studio 工具箱。 You can then embed the player into your app, and access various properties of Media Player, like the url:然后,您可以将播放器嵌入到您的应用程序中,并访问媒体播放器的各种属性,例如 url:

WMPplayer.URL = stringPathToFile;

This solution is possibly not what you want because it's starting a new instance of Media Player (as far as I know), however it might point you in the right direction.此解决方案可能不是您想要的,因为它正在启动 Media Player 的新实例(据我所知),但是它可能会为您指明正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM