1

At first, I want to say that I had read all topics related to my problem here, on stackoverflow (and of course googled), but those research provided no solution to my problem. I'm writing app for Windows Phone and I need to play two sounds simultaneously, but this code doesn't work, because there is slight, but noticeable dealy between two sounds, and there must be NO perceptible delay in my project.

Stream s1 = TitleContainer.OpenStream("C.wav");
Stream s2 = TitleContainer.OpenStream("C1.wav");
SoundEffectInstance sci = sc.CreateInstance();
SoundEffectInstance sci1 = sc1.CreateInstance();
sci.Play();
sci1.Play();

I also tried to perform a simple mix of two wav files, but it doesn't work for a reason that I don't know. (ArgumentException - Ensure that the specified stream contains valid PCM mono or stereo wave data. - is thrown, when calling SoundEffect.FromStream(WAVEFile.Mix(s1, s2));

    public static Stream Mix(Stream in1,Stream in2)
    {
        BinaryWriter bw;
        bw = new BinaryWriter(new MemoryStream());
        byte[] header = new byte[44];
        in1.Read(header, 0, 44);
        bw.Write(header);
        in2.Seek(44, SeekOrigin.Begin);
        BinaryReader r1 = new BinaryReader(in1);
        BinaryReader r2 = new BinaryReader(in2);
        while (in1.Position != in1.Length)
        {
            bw.Write((short)(r1.ReadInt16() + r2.ReadInt16()));
        }
        r1.Dispose();
        r2.Dispose();
        bw.BaseStream.Seek(0, SeekOrigin.Begin);
        return bw.BaseStream;
    }

Stream s1 = TitleContainer.OpenStream("C.wav");
Stream s2 = TitleContainer.OpenStream("C1.wav");
s3 = SoundEffect.FromStream(WAVEFile.Mix(s1, s2));

So, does anyone know how to play two sounds at the time?

AirL
  • 1,887
  • 2
  • 17
  • 17
quirell
  • 245
  • 1
  • 18
  • possible duplicate of [Playing two sounds simultaneously c#](http://stackoverflow.com/questions/5747086/playing-two-sounds-simultaneously-c-sharp) – DotNetRussell Oct 29 '13 at 22:28

1 Answers1

0

So your first solution SHOULD work. I have another solution that is very similar with a twist that I KNOW works.

static Stream stream1 = TitleContainer.OpenStream("soundeffect.wav");
static SoundEffect sfx = SoundEffect.FromStream(stream1);
static SoundEffectInstance soundEffect = sfx.CreateInstance();

public void playSound(){
    FrameworkDispatcher.Update();
    soundEffect.Play();
}

The reason your second solution didnt work is because there are very specific file formats that the windows phone can play.

List of supported formats

http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff462087(v=vs.105).aspx

Reference for this code is my blog

http://www.anthonyrussell.info/postpage.php?name=60

Edit

You can see the above solution working here

http://www.windowsphone.com/en-us/store/app/xylophone/fe4e0fed-1130-e011-854c-00237de2db9e

Edit#2

In response to the comment below that this code doesn't work I have also posted a working, published app on my blog that implements this very code. It's called Xylophone, it's free and you can download the code here at the bottom of the page.

http://anthonyrussell.info/postpage.php?name=60

Community
  • 1
  • 1
DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
  • I've managed to make my "mixing" code work, indeed, it was in other format than I had thought it was. Thanks. – quirell Oct 29 '13 at 09:19
  • I've tested your code, and it works neither;/ And it appears that mixing two wav files lasts for 2 seconds after having clicked button. I will have to come up with a solution how to mix it faster, maybe multithread mixing will help... – quirell Oct 29 '13 at 10:13
  • You can play the sounds at the same time. "mixing " them is very inefficient. I would suggest trying to figure out where you went wrong or why its not working. This code does work. I litteraly copied and pasted it out of the solution for the app I posted above... – DotNetRussell Oct 29 '13 at 11:06
  • Checked your app (and slightly modified) and concluded, that it works just (wrong) like mine:P When I first time press the button to play 2 sounds at the time , they play one after another, when I press second time, they play simultaneously. Funny, there is no hope for me, I have to think of fast mixing. – quirell Oct 29 '13 at 20:53
  • @quirell did you do it on an emulator? If you download it on the phone you can CLEARLY see and more importantly hear that they are playing two sounds at the same time. This is not that complicated of a solution to implement and this is the accepted practice. So I don't know how else to answer your question. – DotNetRussell Oct 29 '13 at 21:34
  • @quirell not to mention this question (and answer) are a duplicate http://stackoverflow.com/questions/5747086/playing-two-sounds-simultaneously-c-sharp?rq=1 – DotNetRussell Oct 29 '13 at 21:34
  • @quirell go download Smuckers Launch (http://www.windowsphone.com/en-us/store/app/smuckers-launch/f239dc2c-e088-47bd-8e34-9a27ffd8684f) a game I made. It has background music playing and plays sounds over top of it using THIS VERY SAME code. – DotNetRussell Oct 29 '13 at 21:36
  • Or even easier just go watch the gameplay video here http://www.youtube.com/watch?v=G3bDps6PE3k&feature=youtu.be – DotNetRussell Oct 29 '13 at 21:38
  • you should use XNAFrameworkDispatcherService - http://msdn.microsoft.com/en-us/library/ff842408.aspx – George Birbilis Jan 14 '15 at 21:34