简体   繁体   English

在Windows Phone上同时播放声音

[英]Playing sounds simultaneously on windows phone

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. 首先,我想说的是,我已经在这里阅读了所有与我的问题相关的主题,有关stackoverflow(当然还有Google搜索),但是这些研究并没有解决我的问题。 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. 我正在为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();

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. 我还尝试了两个wav文件的简单混合,但是由于我不知道的原因,它无法正常工作。 (ArgumentException - Ensure that the specified stream contains valid PCM mono or stereo wave data. - is thrown, when calling SoundEffect.FromStream(WAVEFile.Mix(s1, s2)); (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));

So, does anyone know how to play two sounds at the time? 那么,有谁知道同时播放两种声音吗?

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. 您的第二个解决方案不起作用的原因是Windows Phone可以播放非常特定的文件格式。

List of supported formats 支持格式列表

http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff462087(v=vs.105).aspx 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 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 http://www.windowsphone.com/en-us/store/app/xylophone/fe4e0fed-1130-e011-854c-00237de2db9e

Edit#2 编辑#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. 它称为Xylophone,它是免费的,您可以在页面底部的此处下载代码。

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

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

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