gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:43:26 24 4
gpt4 key购买 nike

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

平台是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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com