简体   繁体   English

停止SoundPlayer播放

[英]Stopping SoundPlayer playback

I am trying to create my own little Soundboard for playing WAV files. 我正在尝试创建自己的小音板来播放WAV文件。 Now the playback isn't a problem, but I want a way to stop playback if required. 现在播放不是问题,但我想要一种方法来停止播放(如果需要)。 I thought of a stop button, but did not know how to make that work. 我想到了一个停止按钮,但不知道如何做到这一点。 So I thought of using a loop but it doesn't seem to work. 所以我想过使用一个循环,但它似乎不起作用。 Just wondering if anyone can help me where I am going wrong or if this is just a fail way to do it, then if you can show me a better way that would be great. 只是想知道是否有人可以帮助我在哪里出错或者如果这只是一种失败的方式,那么如果你能告诉我一个更好的方式。 :) :)

    private void button1Sound(object sender, MouseEventArgs e)
    {
        int i = 0;
        SoundPlayer simpleSound = new SoundPlayer(@"file.wav");

        while (i == 0)
        {
            simpleSound.Play();
            i++;
        }

        if (i >= 1)
        {
            simpleSound.Stop();
        }
    }

I also thought of just doing an ELSE IF but that also didn't work. 我还想过做一个ELSE IF,但这也行不通。 I tried: 我试过了:

    private void button1Sound(object sender, MouseEventArgs e)
    {
        int i = 0;
        SoundPlayer simpleSound = new SoundPlayer(@"file.wav");

        if (i == 0)
        {
            simpleSound.Play();
            i++;
        }
        else if (i > 1)
        {
            simpleSound.Stop();
        }
    }

Because you've added int i = 0 inside the void, every time the event is called, i will always be 0 . 因为你在void中添加了int i = 0 ,所以每次调用事件时, i将始终为0 So it's better to include i outside the void. 因此,最好将i包括在虚空之外。

 int i = 0;
 SoundPlayer simpleSound = new SoundPlayer(@"file.wav"); //Thanks to @Wanabrutbeer

 private void button1Sound(object sender, MouseEventArgs e)
    {


        if (i == 0)
        {
            simpleSound.Play();
            i++;
        }

       else if (i >= 1)
        {
            simpleSound.Stop();
            i--;
        }
    }

NOTICE : The class SoundPlayer only plays .wav files. 注意SoundPlayer类仅播放.wav文件。 If you would like to play other media files such as .mp3, .mpeg, etc... please use MediaPlayer instead. 如果您想播放其他媒体文件,如.mp3,.mpeg等,请使用MediaPlayer

Thanks, 谢谢,

I hope this helps :) 我希望这有帮助 :)

EDIT - editing code fragment to show use of Asynchronous Load 编辑 - 编辑代码片段以显示异步加载的使用

Try this code: 试试这段代码:

private System.Media.SoundPlayer sound;
private bool isPlaying = false;
private System.Drawing.Image playimage = System.Drawing.Image.FromFile(@"playimage");
private System.Drawing.Image stopimage = System.Drawing.Image.FromFile(@"stopimage");

private void playstopbutton_Click(object sender, EventArgs e)
{
    Button btn = (sender as Button);
    if (isPlaying)
    {
        sound.Stop();
        isPlaying = false;
        btn.Text = "Play";
        btn.Image = playimage;
    }
    else if (sound.IsLoadCompleted)
    {
        sound.Play();
        isPlaying = true;
        btn.Text = "Stop";
        btn.Image = stopimage;
    }
    else
    {
        btn.Enabled = false;
        if (sound == null) sound = new System.Media.SoundPlayer(@"wavefile");
        sound.LoadCompleted += new AsyncCompletedEventHandler(sound_LoadCompleted);
        sound.LoadAsync();
    }
}

private void sound_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
    this.playstopbutton.Text = "Stop";
    this.playstopbutton.Image = playimage;
    this.playstopbutton.Enabled = true;
    isPlaying = true;
    sound.Play();
}

The problem you might be running into is your scope for the sound is only inside the method, it needs to be class scope, so that the same instance sound is dealt with each time. 您可能遇到的问题是声音的范围仅在方法内部,它需要是类范围,因此每次处理相同的实例声音。 Technically the int method you used shuold work if you declare the SoundPlayer outside the click handler. 从技术上讲,如果在单击处理程序之外声明SoundPlayer,则使用shuold工作的int方法。 Also, whatever variable you use to track the play state needs to exist outside the handler method so that the value persists on each click action. 此外,用于跟踪播放状态的任何变量都需要存在于处理程序方法之外,以便每次单击操作都保持该值。

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

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