0

我是 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;
        }
    }
4

1 回答 1

0

阅读此 https://docs.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!";
}
于 2021-12-05T16:52:17.980 回答