在Windows Phone上同时播放声音 [英] Playing sounds simultaneously on windows phone

查看:104
本文介绍了在Windows Phone上同时播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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();

我也试过来执行两个WAV文件的简单组合,但它不会为我不知道工作的原因。 (ArgumentException的 - 确保指定的流包含有效PCM单声道或立体声波形数据 - 被抛出,调用SoundEffect.FromStream(WAVEFile.Mix时(S1,S2));

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?

推荐答案

所以,你的第一个解决方案应该工作。我有另一种解决方案是,我知道作品的扭曲非常相似。

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();
}

原因你的第二个解决方案,没有工作是因为有非常具体的文件格式在Windows Phone都可以玩。

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

支持的格式列表

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

参考此code是我的博客

Reference for this code is my blog

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

您可以看到上面的解决方案,在这里工作。

You can see the above solution working here

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

在回应低于这个code中的评论不工作我也贴了一个工作,在我的博客上发表的应用程序,实现了这个非常code。这就是所谓的木琴,它是免费的,你可以在页面的底部在这里下载code。

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

这篇关于在Windows Phone上同时播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆