Background sound of Form

0

I have a method;

    public void SonidoAmbiente()
    {
        sonidoEntradaCarga = new SoundPlayer("Resources\Sonidos\ambient.wav");
        sonidoEntradaCarga.PlayLooping();       
    }

that I launch when starting my application, the audio is perfect, the problem is that I want it to be playing whenever the App is running.

But when throwing another sound (any, in the same way) this background sound stops.

I tried to launch the sound with Task but the result is the same.

    
asked by Hector Lopez 06.04.2018 в 14:44
source

2 answers

1

As much as I tried I could not play both audios at the same time with the Class SoundPlayer I think it seems to use the same instance to play the audio thing that when you do play() it replaces the audio that is already being played (this it is a supposition to not find documentation of the flow that follows). Therefore, I recommend that you use the class MediaPlayer . Then I'll explain how to use it.

Add references to be used

To use this class you must add two references to your project which are PresentationCore and WindowsBase , these references are found in the Assemblies.

Code

Already having the references you only add the using System.Windows.Media; so you can use the class. As a special data you will not need to execute a Thread to reproduce the sounds one over another but by initializing the object you will be able to reproduce it simultaneously.

SOen Source: Play two sounds simultaneusly

using System.Windows.Media;

private void button1_Click(object sender, EventArgs e)
{

    MediaPlayer d = new MediaPlayer();
    d.Open(new System.Uri(@"archivo2.wav"));
    d.Play();

}

private void Form1_Load(object sender, EventArgs e)
{

    MediaPlayer c = new MediaPlayer();
    c.Open(new System.Uri(@"archivo1.wav"));
    c.Play();

}
    
answered by 12.04.2018 / 19:17
source
1

To play both audios you need to use two variables SoundPlayer different

private SoundPlayer sonidoEntradaCarga;

public void SonidoAmbiente()
{
    sonidoEntradaCarga = new SoundPlayer("Resources\Sonidos\ambient.wav");
    sonidoEntradaCarga.PlayLooping();       
}

public void Sonido2()
{
    var sonido2 = new SoundPlayer("Resources\Sonidos\sonido2.wav");
    sonido2.Play();       
}

If you make a new of the existing variable, you would be canceling that reproduction

Playing sounds simultaneously

Playing multiple sounds simultaneously with SoundPlayer

    
answered by 11.04.2018 в 17:47