MP3 在单独的线程中播放
一个问题解决了,另一个问题随之而来:在 C# 程序中,我使用以下方法将标签颜色设置为绿色,然后播放 mp3 文件,最后将颜色设置回黑色。 问题是声音似乎是在额外的线程中播放的,因此两种颜色变化之间的时间太短(事实上,在播放文件时它应该具有绿色)。
private void playSound()
{
label1.ForeColor = Color.LimeGreen;
Application.DoEvents();
WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayer();
wmp.URL = @"C:\examplesound.mp3"; // duration about 30s
wmp.controls.play();
label1.ForeColor = Color.Black;
}
有什么办法可以强制标签在播放 mp3 文件时保持绿色吗?
One problem was solved, another followed: In a C#-program I use following method to set a labels color to green, then playing a mp3-file and finally setting the color back to black.
The problem is that the sound seems to be played in an extra thread, thus the time between the change of the two colors is too short (in fact, it should have the green color while the file is played).
private void playSound()
{
label1.ForeColor = Color.LimeGreen;
Application.DoEvents();
WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayer();
wmp.URL = @"C:\examplesound.mp3"; // duration about 30s
wmp.controls.play();
label1.ForeColor = Color.Black;
}
Is there anything can be done to force the label to keep the green color whilst the mp3-file is played?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要立即将颜色设置回黑色,因为播放是在另一个线程中进行的。
当当前曲目结束时,WMPLib 会发出
PlayStateChange
事件。因此,添加一个处理程序:
的页面playState
有一个值列表:您需要确保这是在 UI 线程上完成的。
Don't set the colour back to black straight away as the playback is in another thread.
When the current track ends WMPLib sends out a
PlayStateChange
event.So add a handler:
The page for
playState
has a list of values:You'll need to make sure this is done on the UI thread.
尝试挂钩 PlayStateChanged 事件并将
label1.ForeColor = Color.Black;
放入其中。目前,您的代码中没有任何内容表明它只应在完成时、仅在开始播放后更改为黑色。
Try hooking the PlayStateChanged event and put the
label1.ForeColor = Color.Black;
in there.At the moment there's nothing in your code saying that it should only change to black when it finishes, only after it has started to play.