尝试从数组中读取时出错

时间:2016-03-08 17:02:52

标签: c#

我正在尝试让我的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;
}

1 个答案:

答案 0 :(得分:6)

您的hdr是一个数组,您需要通过放置方括号然后传递索引来获取条目

binaryWriter.Write(hdr[i]);
相关问题