0
我正在嘗試使用C#控制檯應用程序發出嘟嘟聲。是的,我知道我可以使用Console.Beep,但我也想降低音量等嘗試從數組中讀取時出錯
但我得到的錯誤是這樣的:
方法名稱預計
在這條線:
binaryWriter.Write(hdr(i));
這是我的代碼:
private bool Beep(int volume, int frequency, int duration)
{
try
{
double amplitude = volume * 1.27;
double a = ((amplitude * (System.Math.Pow(2, 15)))/1000) - 1;
double deltaFt = 2 * System.Math.PI * frequency/8000;
double samples = 441 * (duration/100);
int bytes = Convert.ToInt32(samples) * 4;
int[] hdr = {
0x46464952,
36 + bytes,
0x45564157,
0x20746d66,
16,
0x20001,
8000,
176400,
0x100004,
0x61746164,
bytes
};
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(44 + bytes))
{
using (System.IO.BinaryWriter binaryWriter = new System.IO.BinaryWriter(memoryStream))
{
for (int i = 0; i <= hdr.Length - 1; i++)
{
binaryWriter.Write(hdr(i));
}
for (int T = 0; T <= Convert.ToInt32(samples) - 1; T++)
{
short sample = Convert.ToInt16(a * System.Math.Sin(deltaFt * T));
binaryWriter.Write(sample);
binaryWriter.Write(sample);
}
binaryWriter.Flush();
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
using (System.Media.SoundPlayer sp = new System.Media.SoundPlayer(memoryStream))
{
sp.PlaySync();
}
}
}
}
catch
{
return false;
}
return true;
}
使用括號內爲數組,以獲得進入:'HDR [I]' – LarsTech
用方括號的,不是圓,從數組中讀取時 – user1666620