简体   繁体   English

尝试从数组中读取时出错

[英]error when trying to read from an array

I am trying too have my C# console application make a beep sound.我也在尝试让我的 C# 控制台应用程序发出哔哔声。 Yes I know I can use Console.Beep but I also want to lower the volume etc.是的,我知道我可以使用 Console.Beep 但我也想降低音量等。

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]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从Sharepoint存储库读取文件时出现401错误 - Getting 401 error when trying to read file from sharepoint repository Xamarin - 尝试读取JSon时出错 - Xamarin - Error when trying to read JSon 尝试读取过程内存时出现错误299 - Error 299 when trying to read process memory 尝试从SQLiteDataReader读取时遇到ObjectDisposedException - Encountering ObjectDisposedException when trying to read from SQLiteDataReader 尝试从ListBox键值对中读取“值”时出现无效的强制转换错误 - Invalid Cast Error when trying to read “Value” from a ListBox Key-Value pair 尝试重定向标准和/或错误时无输出。 我如何从中读取输出? - No output when trying to redirect standard and/or error. How can i read the output from this? 尝试使用 C# 从文件读取和写入时 WPF 出错 - Error in WPF when trying to read and write from a file using C# 尝试从URL读取标记文件时出现斯坦福POS标记错误 - Stanford POS tagger error when trying to read the tagger file from URL 值已读取,或尝试从 Stream 读取时没有值 - Value already read, or no value when trying to read from a Stream 尝试更新MongoDb数组元素时出错 - Error when trying to update MongoDb array element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM