【问题标题】:C# Get current song source url from Windows Media PlayerC# 从 Windows Media Player 获取当前歌曲源 url
【发布时间】:2013-08-10 04:14:55
【问题描述】:

我正在尝试创建一个小应用程序,通知用户当前在 Windows Media Player 上播放的歌曲的路径。

所以我四处搜索并发现了一个很好的代码:

WMPLib.WindowsMediaPlayer player = new WMPLib.WindowsMediaPlayer();
// Get an interface to the first media item in the library. 
WMPLib.IWMPMedia3 firstMedia = (WMPLib.IWMPMedia3)player.mediaCollection.getAll().get_Item(0);

// Make the retrieved media item the current media item.
player.currentMedia = firstMedia;

// Display the name of the current media item.
currentMediaLabel.Text = ("Found first media item. Name = " + player.currentMedia.name);

但问题是这段代码实际上是获取列表中的第一首歌曲而不是获取当前歌曲,我尝试更改方法但没有好处:(我希望你能帮助我。

【问题讨论】:

    标签: c# api com wmplib


    【解决方案1】:

    您已经在player.currentMedia 中拥有它。

    WMPLib.WindowsMediaPlayer player = new WMPLib.WindowsMediaPlayer();
    
    // start the player
    ...
    
    if(player.currentMedia != null)
    {
        // Display the name of the current media item.
        currentMediaLabel.Text = ("Found first media item. Name = "
                                  + player.currentMedia.name);
    }
    

    【讨论】:

    • 它不起作用,我收到此错误:System.NullReferenceException
    • 这意味着player.currentMedia 为空,如果还没有歌曲正在播放,就是这种情况。在您的代码中,您应该在访问其属性之前验证 player.currentMedia 不为空。
    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多