我一直在关注这些(1、2)的教程来尝试构建一个 .wav 文件。但是,我似乎无法让它工作,因为 wav 文件将正确打开但被列为 0 秒并且不播放任何内容。
代码(这很糟糕,因为它只是一个尝试让它工作的测试):
using System;
using System.IO;
using System.Linq;
using System.Text;
namespace Namespace
{
class Program
{
static void Main()
{
StringBuilder SB = new StringBuilder();
for (int e = 0; e < 200000; e++)
{
SB.Append(" ff");
}
int Size = SB.Length / 3;
StringBuilder SBHexSize = new StringBuilder(Convert.ToString(Size, 16));
while (SBHexSize.Length < 8)
{
SBHexSize.Append("0");
}
string HexSize = SBHexSize.ToString();
const string RIFF = "52 49 46 46";
const string RestOfHeader = "57 41 56 45 66 6d 74 20 10 00 00 00 01 00 02 00 22 56 00 00 88 58 01 00 04 00 10 00 64 61 74 61 00 08 00 00";
//Console.WriteLine($"{RIFF} {HexSize[6..8]} {HexSize[4..6]} {HexSize[2..4]} {HexSize[..2]} {RestOfHeader}{SB.ToString()}");
//System.Threading.Thread.Sleep(-1);
//ByteArray bytes = new ByteArray($"{RIFF} {HexSize[6..8]} {HexSize[4..6]} {HexSize[2..4]} {HexSize[..2]} {RestOfHeader}{SB.ToString()}");
ByteArray bytes = new ByteArray($"{RIFF} FF FF FF FF {RestOfHeader}{SB.ToString()}");
bytes.Write();
}
}
class ByteArray
{
private byte[] array;
public ByteArray(string hex)
{
array = Enumerable.Range(0, hex.Length)
.Where(x => x % 3 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
public void Write()
{
File.WriteAllBytes(@"C:\Users\name\source\repos\MusicCreator\MusicCreator\musictest.wav", array);
}
}
}
是代码有问题,比如我如何尝试写入字节,还是字节本身有问题?