【问题标题】:WmpLib player.pause not workingWmpLib player.pause 不工作
【发布时间】:2014-07-01 16:13:39
【问题描述】:

所以我想在 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();
}

正在调用的播放器启动媒体播放器,但不会暂停。 “控制在这里”被打印两次,但播放器没有暂停。 但是当我在两个不同的按钮中显示它时它工作正常。 例如。

private void button2_Click(object sender, EventArgs e)
{
  wmp.controls.play();            
}

private void button3_Click(object sender, EventArgs e)
{
  wmp.controls.pause();
}

我想开发一个功能来播放一个wav文件一段时间然后停止然后再播放一段时间然后再停止。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    这应该对你有帮助。首先在窗体上放置一个 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();
                }
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2016-01-21
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 2021-01-10
      相关资源
      最近更新 更多