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

时间:2015-06-18 06:34:05

标签: 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);
            }
        }


    }

1 个答案:

答案 0 :(得分:1)

这是我做的 - 只需创建一个新的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();