0

I use soundeffect to play sound from byte array and all things go alright, but i cant stop playing the sound because no method called stop() in Soundeffect, how i can stop it ?

The code:

private void playFile(byte[] file)
    {
        try
        {

            if (file == null || file.Length == 0) return;


            SoundEffect se = new SoundEffect(file, microphone.SampleRate, AudioChannels.Mono);
            SoundEffect.MasterVolume = 0.7f;
            if (stop != 1)
            {
                FrameworkDispatcher.Update();
                se.Play();
            }
            else
            {
                //Here should stop, how !
            }
        }
        catch (Exception ee)
        {
            MessageBox.Show(ee.Message);
        }
    }
Belal Ghanem
  • 97
  • 1
  • 11
  • From your code I'm not sure what you are trying to stop in the else since nothing is actually started (since the play is in the if of the same condition) – Benoit Catherinet Sep 24 '13 at 18:24
  • no it's work, and all thing go alright .. but i don't know how to stop it . – Belal Ghanem Sep 24 '13 at 20:32
  • 1
    my question is what are you stoping there since in the code you published you have "SoundEffect se" as a local variable so next time playFile is called it will just create a new one and you will stop a new SoundEffect instead of the original one – Benoit Catherinet Sep 24 '13 at 20:53

3 Answers3

0

Have you tried calling .Dispose() on your SoundEffect object? According to the documentation,

When a SoundEffect object is destroyed, all SoundEffectInstance objects previously created by that SoundEffect will stop playing and become invalid.

Additionally, Microsoft recommends using MediaElement instead, unless you're developing with XNA Game Studio 4.0. So if that applies to you, you may want to consider using MediaElement which does have Play, Pause, and Stop functions.

lhan
  • 4,585
  • 11
  • 60
  • 105
0

try using SoundEffectInstance

Sound effect instance

Here's a link to the solution

Sound Effect Instance

More over SoundEffectInstance implements IDisposeable so it will have a dispose() method as well as stop() for the sound effect .

Anobik
  • 4,841
  • 2
  • 17
  • 32
0

Create a variable for SoundEffectInstance as shown below

SoundEffectInstance soundEffectInstance;

then create an instance of your soundEffect after loading the file..

soundEffectInstance = se.CreateInstance();

after that use this soundEffectInstance to Stop, Play or Pause where ever you want in particular scope.

soundEffectInstance.Play();

Note that SoundEffect is used for fire and forget type instances, if you want to control any particular instance of a SoundEffect you will need to use SoundEffectInstance.

I agree with Benoit Catherinet. If you are trying to stop a particular sound on some event then you must place the stoping code in that event method and for that your SoundEffect instance must be global and not local.

We can better help you if you can explain about what you are trying to achieve.

parveen
  • 1,939
  • 1
  • 17
  • 33