简体   繁体   中英

error when trying to read from an array

I am trying too have my C# console application make a beep sound. Yes I know I can use Console.Beep but I also want to lower the volume etc.

But the error I am getting is this:

Method name expected

on this line:

binaryWriter.Write(hdr(i));

This is my code:

    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 是一个数组,您需要通过放置方括号然后传递索引来获取条目

binaryWriter.Write(hdr[i]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM