以编程方式从 Windows Media Player 获取歌曲的曲目长度

发布于 2024-12-12 01:48:20 字数 265 浏览 0 评论 0原文

我想获得一首歌的曲目长度。当我调试它时,我能够获取曲目长度并将其设置到名为 SongDuration 的字符串变量中。然而,当我实际执行它时,它不起作用,字符串为空。我该如何让它发挥作用?我尝试先初始化变量,但结果仍然相同。我缺少什么?当我逐行调试它时它可以工作,只是不运行它。

这是一段代码:

wplayer.controls.play();
songDuration = wplayer.currentMedia.durationString;

I want to get the track length of a song. When i debug it, i was able to get the track length and set it into a string variable called songDuration. However when i actually execute it, it does not work, the string is null. how do i make it work? I have tried initializing the variables first but still same result. What am i missing? It works when i debug it line by line, just not running it.

Heres the piece of code:

wplayer.controls.play();
songDuration = wplayer.currentMedia.durationString;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薆情海 2024-12-19 01:48:20

这应该是您正在寻找的属性,它接受双精度值,因此您需要考虑如何使用目前拥有的 SongDuration 字符串变量:

player.controls.当前位置

这篇 MSDN 文章 应该为您提供更多信息,尽管示例指向在网页中使用该控件,但我相信内容应该类似。

另外这个所以问题看起来很相似,但包含答案已经。

This should be the property you are looking for, it accepts a double so you will need to take into consideration how you can use the songDuration string variable you have at the moment:

player.controls.currentPosition

This MSDN article should give you some more information, although the examples points towards using the control in a web page I believe the content should be similar.

Additionally this SO question appears to be similar but contains an answer already.

噩梦成真你也成魔 2024-12-19 01:48:20

label3.Text = (wmp.Ctlcontrols.currentPosition).ToString();
label4.Text = wmp.Ctlcontrols.currentItem.duration.ToString();

label3.Text = (wmp.Ctlcontrols.currentPosition ).ToString();
label4.Text = wmp.Ctlcontrols.currentItem.duration.ToString();

镜花水月 2024-12-19 01:48:20

我发现调用 player.controls.play() 的方法必须退出,并且播放器必须实际开始播放几分之一秒,然后 .duration 和 .durationString 属性将返回正确的值,而不是空字符串和 0

。不起作用:

wplayer.controls.play();
songDuration = wplayer.currentMedia.durationString;

这也不起作用:

wplayer.controls.play();
Application.DoEvents();
System.Threading.Thread.Sleep(100000);
Application.DoEvents();
songDuration = wplayer.currentMedia.durationString;

我解决了这个问题,方法是开始播放媒体并退出该方法,创建一个每 100 毫秒触发一次的计时器事件,每次调用它时,它都会检查持续时间是否仍然为 0,当它是不,它可以捕获持续时间并暂停媒体。另一种方法是使用 AxWindowsMediaPlayer,您可以在媒体状态更改时添加事件处理程序,并且当它开始播放时,您可以捕获该事件并查看持续时间。我没有走那条路,也不想采取步骤来导入正在使用的任何命名空间。也就是说,这就是 MSFT 的建议:

// Add a delegate for the PlayStateChange event.
player.PlayStateChange += new 
AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(player_PlayStateChange);

我没有安装 SDK,而是采用了这种方式。
像这样启动播放器

System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
myTimer.Tick += new EventHandler(GetDuration);
myTimer.Interval = 100;
wplayer.controls.play();
return;

// Check for duration in this other routine which runs every 100 msec until 
// Media Player tells us the duration.

private string GetDuration()
{
    // public variable songDuration declared elsewhere
    songDuration = wplayer.currentMedia.durationString;
    if (songDuration.Length > 0) wplayer.controls.pause();
}

I find that the method that calls player.controls.play() must exit and the player must actually start playing for a fraction of a second before the .duration and .durationString properties will return correct values and not an empty string and 0.

This will not work:

wplayer.controls.play();
songDuration = wplayer.currentMedia.durationString;

This also will not work:

wplayer.controls.play();
Application.DoEvents();
System.Threading.Thread.Sleep(100000);
Application.DoEvents();
songDuration = wplayer.currentMedia.durationString;

I solved it by starting to play the media and exiting the method, creating a Timer event that triggers every 100 msec and each time it is called, it checks if duration is still 0, when it is not, it can capture the duration and pause the media. Another approach is to use AxWindowsMediaPlayer where you can add an event handler when the media state changes and when it starts playing, you can trap that event and see the duration. I did not go that route and did not want to take the steps to import whatever namespace that was using. That said, this is what MSFT suggests:

// Add a delegate for the PlayStateChange event.
player.PlayStateChange += new 
AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(player_PlayStateChange);

Instead of installing the SDK, I went about it this way.
Start the player like this

System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
myTimer.Tick += new EventHandler(GetDuration);
myTimer.Interval = 100;
wplayer.controls.play();
return;

// Check for duration in this other routine which runs every 100 msec until 
// Media Player tells us the duration.

private string GetDuration()
{
    // public variable songDuration declared elsewhere
    songDuration = wplayer.currentMedia.durationString;
    if (songDuration.Length > 0) wplayer.controls.pause();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文