我在 rawData1 中拥有所有数据,它是短数组。我已将其转换为字节数组。然后我创建了 wav 文件头,然后将该头和字节数组数据写入文件中。但我收到错误“Windows 媒体播放器在播放文件时出现问题”。任何人都可以帮助我吗?
public void save file
{
byte[] byte_array = new byte[rawData1.Length * 2];
for (int i = 0; i < rawData1.Length; ++i)
{
//byte_array[2 * i] = getByte1(rawData1[i]);
//byte_array[2 * i + 1] = getByte2(rawData1[i]);
byte_array[2 * i] = getByte2(rawData1[i]);
byte_array[2 * i + 1] = getByte1(rawData1[i]);
}
uint numsamples = 44100;
ushort numchannels = 2;
ushort samplelength = 1; // in bytes
uint samplerate = 22050;
StorageFolder storageFolder = KnownFolders.MusicLibrary;
StorageFile file = await storageFolder.CreateFileAsync("sample.wav", CreationCollisionOption.GenerateUniqueName);
Stream stream = await file.OpenStreamForWriteAsync();
BinaryWriter wr = new BinaryWriter(stream);
wr.Write("RIFF".ToCharArray());
wr.Write(36 + numsamples * numchannels * samplelength);
wr.Write("WAVEfmt".ToCharArray());
wr.Write(16);
wr.Write((ushort)1);
wr.Write(numchannels);
wr.Write(samplerate);
wr.Write(samplerate * samplelength * numchannels);
wr.Write(samplelength * numchannels);
wr.Write((ushort)(8 * samplelength));
wr.Write("data".ToCharArray());
wr.Write(numsamples * samplelength);
wr.Write(byte_array);
wr.Dispose();
stream.Dispose();
}
public static byte getByte1(short s)
{
return (byte)s;
}
public static byte getByte2(short s)
{
int temp= s >> 8;
return (byte)temp;
}
有谁能够帮我?