簡體   English   中英

嘗試從數組中讀取時出錯

[英]error when trying to read from an array

我也在嘗試讓我的 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 是一個數組,您需要通過放置方括號然后傳遞索引來獲取條目

binaryWriter.Write(hdr[i]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM