C#, WMPLib - How to check if the player is playing or not to trigger another process

1.1k Views Asked by At

I'm new to C#. I want to use only 1 timer to do 2 events with different interval (0.2s and 1s). 0.2s interval is for checking error in system and it cannot stop. 1s interval is for playing sound if an error occurred. So, now I'm using a calculation to skip calling function checkError() because if I don't like this the sound won't play properly because of the duration of mp3 file but I would like to use check state of player if it's still playing or not instead of this calculation. For example, it will call checkError() again after finished play sound. What should I do ?

Ps. Sorry for my English :(

    WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayer();
    int count = 0;
    int errorFlag = 0;
    int called = 0;

    public Form1()
    {
        InitializeComponent();
        timer1.Start();

    }

    private void button5_Click(object sender, EventArgs e)
    {
        if (errorFlag == 0)
            errorFlag = 1;
    }

    private void button6_Click(object sender, EventArgs e)
    {
        if (errorFlag == 1)
            errorFlag = 0;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (errorFlag == 1)
        {
            checkError();
            if (called == 0)
                checkError();
        }

    }

    private void checkError()
    {
        if (called == 0)
        {
            Console.WriteLine(WMPLib.WMPPlayState.wmppsPlaying);
            called = 1;
            wmp.URL = "C:/Users/ndrs-tnp/Desktop/エラー音.mp3";
            wmp.controls.play();
        }
        count += 1;
        if (count == 5)
        {
            called = 0;
            count = 0;
        }
    }
1

There are 1 best solutions below

0
ChickChuck2 On

read this https://learn.microsoft.com/en-us/windows/win32/wmp/axwmplib-axwindowsmediaplayer-playstate--vb-and-c

if (player.playState == WMPLib.WMPPlayState.wmppsPlaying)
{
    playStateLabel.Text = "Windows Media Player is playing!";
}
else
{
    playStateLabel.Text = "Windows Media Player is NOT playing!";
}