在本地级别停止 SoundPlayer 循环

发布于 2024-09-03 06:36:31 字数 976 浏览 5 评论 0原文

我正在努力设置一个警报,该警报会作为对话框弹出,其中包含用户可以选择的多个声音文件选项。我遇到的问题是在本地级别创建一个声音播放器,我可以用按钮关闭它。我遇到的问题是,当我关闭表单时,声音不断循环,因为按钮单击事件中不存在 SoundPlayer。

这就是我所拥有的:

 void callsound()
    {
        if (SoundToggle == 0) // if sound enabled
        {

            if ((SoundFile == 0) && (File.Exists(@"attention.wav")))
            {
                System.Media.SoundPlayer alarm = new System.Media.SoundPlayer(@"attention.wav");
                alarm.PlayLooping();
            }
       if ((SoundFile == 1) && (File.Exists(@"aahh.wav")))
            {
                System.Media.SoundPlayer alarm = new System.Media.SoundPlayer(@"aahh.wav");
                alarm.PlayLooping();
            }
     }

private void button1_Click(object sender, EventArgs e)
    {
        //alarm.Stop();  Only works if SoundPlayer declared at class level
        this.Close();
    }

有没有办法通过声明我所在的 SoundPlayer 实例来完成我想做的事情?或者有没有办法在类级别声明它,并且仍然能够根据用户设置更改声音文件?

I'm working on setting up an alarm that pops up as a dialog with multiple sound file options the user can choose from. The problem I'm having is creating a sound player at the local level that I can close with a button. The problem I'm having is that the sound keeps looping when I close the form because the SoundPlayer doesn't exist within the button click event.

here's what I have:

 void callsound()
    {
        if (SoundToggle == 0) // if sound enabled
        {

            if ((SoundFile == 0) && (File.Exists(@"attention.wav")))
            {
                System.Media.SoundPlayer alarm = new System.Media.SoundPlayer(@"attention.wav");
                alarm.PlayLooping();
            }
       if ((SoundFile == 1) && (File.Exists(@"aahh.wav")))
            {
                System.Media.SoundPlayer alarm = new System.Media.SoundPlayer(@"aahh.wav");
                alarm.PlayLooping();
            }
     }

private void button1_Click(object sender, EventArgs e)
    {
        //alarm.Stop();  Only works if SoundPlayer declared at class level
        this.Close();
    }

Is there a way I can do what I want to do by declaring the SoundPlayer instances where I am? Or is there a way to declare it at the class level, and still be able to change the sound file based on user settings?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的呼吸 2024-09-10 06:36:31

为什么这是一个问题?无论如何,SoundPlayer 不支持同时播放多种声音。将其移至类范围,重写 OnFormClosing 事件,问题解决。

public partial class Form1 : Form {
  private System.Media.SoundPlayer alarm;

  protected override void OnFormClosing(CancelEventArgs e) {
    if (alarm != null) alarm.Stop();
  }
}

Why is this a problem? SoundPlayer doesn't support playing more than one sound at the same time anyway. Move it to class scope, override OnFormClosing event, problem solved.

public partial class Form1 : Form {
  private System.Media.SoundPlayer alarm;

  protected override void OnFormClosing(CancelEventArgs e) {
    if (alarm != null) alarm.Stop();
  }
}
行雁书 2024-09-10 06:36:31

您可以尝试:

SoundMixer.stopAll();

SoundMixer.soundTransform = new SoundTransform(0);

SoundMixer 类控制全局声音,因此它应该停止同一安全沙箱中的所有内容。

You can try:

SoundMixer.stopAll();

or

SoundMixer.soundTransform = new SoundTransform(0);

The SoundMixer class controls global sound, so it should stop everything in the same security sandbox.

对你的占有欲 2024-09-10 06:36:31

使用 Powershell,我正在播放 .wav 文件,并使用以下命令开始重复循环:

$Playwav = new-object ('Media.SoundPlayer') $playpath1 
$Playwav.PlayLooping()

我通过运行部分 (F8) 停止了 Powershell IDE 内的循环:

$Playwav = new-object ('Media.SoundPlayer') $playpath1 
$Playwav.PlayLooping()
$Playwav.stop()

FWIW,我的示例中的 .wav 是 FPS Doug takethatbitch.wav。所以失控的循环让我笑了。

希望这有帮助。

Using Powershell, I was playing with playing .wav files and started a repeating loop using the following:

$Playwav = new-object ('Media.SoundPlayer') $playpath1 
$Playwav.PlayLooping()

I stopped the loop inside Powershell IDE by Run Section (F8):

$Playwav = new-object ('Media.SoundPlayer') $playpath1 
$Playwav.PlayLooping()
$Playwav.stop()

FWIW, the .wav in my example was FPS Doug takethatbitch.wav. So the runaway loop made me laugh.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文