WMPLib often stops playing

走远了吗. 提交于 2019-12-11 02:07:26

问题


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();
    }

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.

Edit: It actually stops playing about a tenth of a second after I move my mouse.


回答1:


You need to also code in the player states like this.

Player = new WMPLib.WindowsMediaPlayer();
            Player.PlayStateChange +=
                new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange);
            Player.MediaError +=
                new WMPLib._WMPOCXEvents_MediaErrorEventHandler(Player_MediaError);
            Player.URL = "FC.wav";
            Player.controls.play();



   private void Player_PlayStateChange(int NewState)
   {
       if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
       {

       }
   }

   private void Player_MediaError(object pMediaObject)
   {
       MessageBox.Show("Cannot play media file.");
       this.Close();
   }



回答2:


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 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();
}

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();
    }
}


来源:https://stackoverflow.com/questions/14350024/wmplib-often-stops-playing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!