所以我尝试在C#中播放一个文件一段时间,这个函数是
void playfile(int duration)
{
wmp.controls.play();
Console.WriteLine("control is here");
System.Threading.Thread.Sleep(duration);
Console.WriteLine("control is here");
wmp.controls.pause();
}呼叫中的播放器启动媒体播放器,但不暂停。"Control is here“被打印了两次,但是播放器并没有暂停。但是,当我在两个不同的按钮中显示它时,它工作得很好。例如:
private void button2_Click(object sender, EventArgs e)
{
wmp.controls.play();
}
private void button3_Click(object sender, EventArgs e)
{
wmp.controls.pause();
}我想开发一个功能,播放一个wav文件一段时间,然后停止,然后再次播放一段时间,然后再次停止。
发布于 2014-07-02 01:07:28
这应该有助于you.First在窗体上放置一个windowsmediaplayer控件,然后处理它的openstatechanged事件,在我的代码中,你会看到名为duration的变量,你可以根据需要设置它的值(从上到下,文本框...),然后代码就不言自明了:
public partial class Form1 : Form
{
Timer t = new Timer();
int duration = 0;
public Form1()
{
InitializeComponent();
}
void t_Tick(object sender, EventArgs e)
{
if (axWindowsMediaPlayer1.Ctlcontrols.currentPosition >= duration)
{
axWindowsMediaPlayer1.Ctlcontrols.stop();
duration = 0;//reset it...
}
}
private void button1_Click(object sender, EventArgs e)
{
duration = 10;/* get the value from wherever you need to...*/
axWindowsMediaPlayer1.URL = @"E:\music\Mike Terrana\Shadows of the Past\01 Pleasure Cube.mp3";
axWindowsMediaPlayer1.Ctlcontrols.play();
}
private void Form1_Load(object sender, EventArgs e)
{
t.Interval = 500;
t.Tick += new EventHandler(t_Tick);
axWindowsMediaPlayer1.OpenStateChange += new AxWMPLib._WMPOCXEvents_OpenStateChangeEventHandler(axWindowsMediaPlayer1_OpenStateChange);
}
void axWindowsMediaPlayer1_OpenStateChange(object sender, AxWMPLib._WMPOCXEvents_OpenStateChangeEvent e)
{
if (axWindowsMediaPlayer1.openState == WMPLib.WMPOpenState.wmposMediaOpen)
{
t.Start();
}
}
}https://stackoverflow.com/questions/24514964
复制相似问题