以编程方式从 Windows Media Player 获取歌曲的曲目长度
我想获得一首歌的曲目长度。当我调试它时,我能够获取曲目长度并将其设置到名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该是您正在寻找的属性,它接受双精度值,因此您需要考虑如何使用目前拥有的 SongDuration 字符串变量:
这篇 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:
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.
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();
我发现调用 player.controls.play() 的方法必须退出,并且播放器必须实际开始播放几分之一秒,然后 .duration 和 .durationString 属性将返回正确的值,而不是空字符串和 0
。不起作用:
这也不起作用:
我解决了这个问题,方法是开始播放媒体并退出该方法,创建一个每 100 毫秒触发一次的计时器事件,每次调用它时,它都会检查持续时间是否仍然为 0,当它是不,它可以捕获持续时间并暂停媒体。另一种方法是使用 AxWindowsMediaPlayer,您可以在媒体状态更改时添加事件处理程序,并且当它开始播放时,您可以捕获该事件并查看持续时间。我没有走那条路,也不想采取步骤来导入正在使用的任何命名空间。也就是说,这就是 MSFT 的建议:
我没有安装 SDK,而是采用了这种方式。
像这样启动播放器
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:
This also will not work:
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:
Instead of installing the SDK, I went about it this way.
Start the player like this