问题描述
我通过以下方法播放mp3和m4as:
I play mp3s and m4as with the following method:
private void playmp3(string path)
{
WMPLib.WindowsMediaPlayer a = new WMPLib.WindowsMediaPlayer();
a.URL = path;
a.controls.play();
}
通常,当我播放它们时,它们只播放大约5秒钟或更短的时间,然后停止播放.如果我以任何方式与(WPF)表单进行交互,它也会停止.我从BackgroundWorker
呼叫playmp3
.
Usually when I play them, they only play for around 5 seconds or less and then stop playing. If i interact with the (WPF) form in any way, it also stops. I call playmp3
from a BackgroundWorker
.
移动鼠标后,它实际上停止播放约十分之一秒.
It actually stops playing about a tenth of a second after I move my mouse.
推荐答案
以简单的方式.宣布您是班级的玩家.
in simple way. declare you player in class level.
WMPLib.WindowsMediaPlayer a;
private void playmp3(string path)
{
a = new WMPLib.WindowsMediaPlayer();
a.URL = path;
a.controls.play();
}
通过这种方式,您可以轻松解决问题
in this way you can resolve the problem easily
如果您使用相同的方法停止播放,则在其中使用if条件,例如
if you using the same method to stop playing like that stuffs using a if condition in it like this
private void playmp3(string path)
{
a = new WMPLib.WindowsMediaPlayer();
a.URL = path;
a.controls.play();
}
您需要添加新的WMPLib.WindowsMediaPlayer();同样在类级别,每当您调用该方法时,它都会创建一个新的播放器实例并尝试停止或播放它,因此应这样做.
you need to add the new WMPLib.WindowsMediaPlayer(); also in the class level, other wise every time you call the method it create a new player instance and try to stop or play it so do it like this.
WMPLib.WindowsMediaPlayer a= new WMPLib.WindowsMediaPlayer();
private void playmp3(string path, string playState)
{
a.URL = path;
if(playstate.Equals("Play))
{
a.controls.play();
}
else if (playState.Equals("Stop"))
{
a.controls.stop();
}
}
这篇关于WMPLib经常停止播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!