简体   繁体   English

每秒在C#Windows窗体应用程序中的自定义.wav文件

[英]Custom .wav file in c# windows form application every second

I am having some trouble playing a 4second long wave... What I am currently doing is running a timer.... 我在播放4秒长的波浪时遇到了一些麻烦...我目前正在做的是运行计时器....

So the timer is set to a second intervals... So every second, I run off and check something... If this check fails.. I play a wav file saying "Get back to work!"... 因此将计时器设置为一秒钟的间隔...因此,每隔一秒钟,我就会跑起来检查一下...如果检查失败。.我播放一个wav文件,说:“恢复工作!” ...

Currently, it pauses the timer.... So I hear "Get back to work" but while it is playing, I have lost 4 seconds of count time, because it is still finishing playing the sound.... Here is my call and my function... 当前,它暂停计时器。...所以我听到“恢复工作”,但是在播放时,我已经失去了4秒钟的计数时间,因为它仍在播放声音。...这是我的电话和我的职能...

playSimpleSound();

    private void playSimpleSound()
    {
        SoundPlayer simpleSound = new SoundPlayer(@"c:\Windows\Media\shortwav.wav");
        simpleSound.PlaySync();
    }

If I switch them out, so that it actually plays everytime it hits.... I only hear the beginning of the wav file.... 如果我将它们切换出来,则每次点击时它都会真正播放...。我只听到wav文件的开头。

playSimpleSound();

    private void playSimpleSound()
    {
        SoundPlayer simpleSound = new SoundPlayer(@"c:\Windows\Media\shortwav.wav");
        simpleSound.Play();
    }

So my question is... 所以我的问题是

How can I continue counting, and play the whole wav file? 如何继续计数并播放整个wav文件? Should I figure out how long the wav file is and then go ahead and do some kind of count with a mod on it? 我应该算出wav文件有多长时间,然后继续并对其进行mod计数吗?

So that I basically only play the file every x amount of seconds or something? 这样我基本上只每隔x秒钟播放文件一次?

So basically just call the playsound function everytime, but inside that function count how many times it has been visited and only play it on the 4th second? 因此,基本上每次都只调用playound函数,但是在该函数内部计算它被访问了多少次并仅在第4秒播放?

You could do something like this...play the sound on a different thread and toggle a flag: 您可以执行以下操作...在其他线程上播放声音并切换标志:

public partial class Form1 : Form
{

    private SoundPlayer simpleSound; 
    private bool SoundPlaying = false;

    public Form1()
    {
        InitializeComponent();
        this.Load += Form1_Load;
    }

    void Form1_Load(object sender, EventArgs e)
    {
        simpleSound = new SoundPlayer(@"c:\Windows\Media\shortwav.wav");
        simpleSound.LoadAsync();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Console.WriteLine("Tick");
        if (true) // check your condition
        {
            this.PlaySound();
        }
    }

    private void PlaySound()
    {
        if (!this.SoundPlaying)
        {
            Console.WriteLine("Starting Sound");
            this.SoundPlaying = true;
            Task.Factory.StartNew(() => {
                simpleSound.PlaySync();
                this.SoundPlaying = false;
            });
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM