c# - 在Windows Phone上同时播放声音

标签 c# windows-phone-7 audio windows-phone-8

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

那么,有谁知道同时播放两种声音吗?

最佳答案

因此,您的第一个解决方案应该有效。我有一个非常相似的解决方案,但我知道它可以起作用。

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可以播放非常特定的文件格式。
支持格式列表
http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff462087(v=vs.105).aspx
此代码的引用是我的博客
http://www.anthonyrussell.info/postpage.php?name=60
编辑
您可以看到上述解决方案在这里工作
http://www.windowsphone.com/en-us/store/app/xylophone/fe4e0fed-1130-e011-854c-00237de2db9e
编辑#2
为了回应下面的评论,该代码不起作用,我还在我的博客上发布了一个可以正常运行的已发布应用程序,该应用程序实现了该代码。它称为Xylophone,它是免费的,您可以在页面底部的此处下载代码。
http://anthonyrussell.info/postpage.php?name=60

关于c# - 在Windows Phone上同时播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19645518/

相关文章:

audio - 无法为iOS 8 Swift播放音频文件

javascript - html5 <audio> 播放时用javascript设置边框颜色

silverlight - 限制 Web 浏览器在 Windows Phone 7 中流式传输或下载文件

windows-phone-7 - 这个字节数组是怎么回事?

c# - MS Test 是否提供默认值等于比较?

c# - 将处理程序添加到我的组件父窗体的所有控件

c# - Windows 手机 : date picker and button

windows - 如何通过Python静音麦克风

c# - 当维度大于 1,1 时指定月历中的第一个月

c# - 如何使用带有 c# 的 google sheets api 将 csv 文件上传到 google sheets?