C#,WMPLib - 如何检查播放器是否正在播放以触发另一个进程



我是C#的新手。我只想使用 1 个计时器来执行 2 个不同间隔(0.2 秒和 1 秒)的事件。0.2s间隔用于检查系统中的错误,它不能停止。1s 间隔用于在发生错误时播放声音。所以,现在我正在使用一个计算来跳过调用函数checkError()因为如果我不喜欢这样,由于 mp3 文件的持续时间,声音将无法正常播放,但我想使用检查播放器的状态是否仍在播放而不是这个计算。例如,它会在播放完声音后再次调用checkError()。 我该怎么办?

对不起我的英语:(

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

阅读此内容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!";
}

最新更新