c# - 定期从内存流播放音频 C#

标签 c# audio windows-phone-8.1 memorystream

我有一些缓冲区,其中包含我想定期从内存流中播放的窦样本。有什么有效的方法可以在没有时间间隔的情况下播放它吗?我尝试制作自己的信号发生器(我知道有一些库提供这种信号,但我想自己生成)。

平台是Windows Phone 8.1 silverlight

更新:代码取自本论坛某处

public static void PlayBeep(UInt16 frequency, int msDuration, UInt16 volume = 16383)
    {
        var mStrm = new MemoryStream();
        BinaryWriter writer = new BinaryWriter(mStrm);
        const double TAU = 2 * Math.PI;           
        int samplesPerSecond = 44100;

        {
            double theta = frequency * TAU / (double)samplesPerSecond;
            // 'volume' is UInt16 with range 0 thru Uint16.MaxValue ( = 65 535)
            // we need 'amp' to have the range of 0 thru Int16.MaxValue ( = 32 767)
            double amp = volume >> 2; // so we simply set amp = volume / 2
            for (int step = 0; step < samples; step++)
            {
                short s = (short)(amp * Math.Sin(theta * (double)step));
                writer.Write(s);
            }
        }


    }

最佳答案

我是这样做的 - 只需创建一个新的 SoundEffectInstance 对象并将其设置为 SoundEffect.CreateInstance 的返回值。

https://msdn.microsoft.com/en-us/library/dd940203.aspx

SoundEffect mySoundPlay = new SoundEffect(mStrm.ToArray(), 16000,AudioChannels.Mono);
SoundEffectInstance instance = mySoundPlay.CreateInstance();
instance.IsLooped = true;
instance.Play();

关于c# - 定期从内存流播放音频 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30908073/

相关文章:

java - libgdx 播放声音或音乐 - 最佳实践

iphone - 同时播放两种声音,其中一种通过蓝牙发送 - iOS 和 Android

c# - 如何将文件从本地存储传递到 Windows Phone 8.1 中的库?

c# - ASP.NET MVC 输出缓存 "thread safe"

c# - 无法在 VS 2015 内存使用分析器中找到预期的对象

java - 有什么想法为什么录制音频会这么慢我的整个应用程序?

c# - 在C#WinPhone 8.1 App中播放声音-有时会导致异常

c# - 在 Converter 中显示 StorageItem 缩略图

c# - 强制 WPF 应用程序在 Windows 启动时最大化

c# - 使用 Visual FoxPro 编程/建立数据库