在Windows Phone上同时播放声音(Playing sounds simultaneousl

2019-10-18 21:03发布

首先,我想说的是,我已经读到这里涉及到我的问题所有主题,在计算器(当然还有一派的),但这些研究没有提供任何解决我的问题。 我正在写的Windows Phone应用程序,我需要同时播放两个声音,但是这个代码不工作,因为那里是很小的,但是两者之间明显的迪利的声音,而且必须在我的项目中没有明显的延迟。

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

我也试图进行两个wav文件的简单组合,但它不会为我不知道工作的原因所在。 (ArgumentException的 - 确保指定的流包含有效PCM单声道或立体声波数据 - 被抛出,调用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));

因此,没有人知道如何在同时播放两个声音?

Answer 1:

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



文章来源: Playing sounds simultaneously on windows phone