C# 反对已处理异常 - 我该如何纠正它们
本文关键字:何纠正 处理 异常 | 更新日期: 2023-09-27 18:35:16
我是初学者 c#。我想将程序从.wav文件转换为.raw文件。我找到了一些来源,我想使用它。但是代码中发生了一些事情。该错误与 ObjectDisposedException 有关。
你能给我一些代码或想法吗?整个代码在下面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace WaveTestRead
{
class WaveReader
{
FileInfo m_fInfo;
FileStream m_fStream;
BinaryReader m_binReader;
// RIFF chunk
byte[] chunkID;
UInt32 chunkSize;
byte[] format;
// fmt subchunk
byte[] fmtChunkID;
UInt32 fmtChunkSize;
UInt16 audioFormat;
UInt16 numChannels;
UInt32 sampleRate;
UInt32 byteRate;
UInt16 blockAssign;
UInt16 BitsPerSample;
// data subchunk
byte[] dataChunkID;
UInt32 dataChunkSize;
byte[] data8L; // 8-bit left channel
byte[] data8R; // 8-bit right channel
Int16[] data16L; // 16-bit left channel
Int16[] data16R; // 16-bit right channel
int numSamples;
public WaveReader()
{
}
public bool Open(String filename)
{
string str;
m_fInfo = new FileInfo(filename);
m_fStream = m_fInfo.OpenRead();
m_binReader = new BinaryReader(m_fStream);
chunkID = new byte[4];
format = new byte[4];
chunkID = m_binReader.ReadBytes(4);
chunkSize = m_binReader.ReadUInt32();
format = m_binReader.ReadBytes(4);
str = System.Text.ASCIIEncoding.ASCII.GetString(chunkID, 0, 4);
if (str != "RIFF")
return false;
str = System.Text.ASCIIEncoding.ASCII.GetString(format, 0, 4);
if (str != "WAVE")
return false;
if (ReadFmt() == false)
return false;
if (ReadData() == false)
return false;
m_fStream.Close();
return true;
}
private bool ReadFmt()
{
fmtChunkID = new byte[4];
fmtChunkID = m_binReader.ReadBytes(4);
string str = System.Text.ASCIIEncoding.ASCII.GetString(fmtChunkID, 0, 4);
if (str != "fmt ")
return false;
fmtChunkSize = m_binReader.ReadUInt32();
audioFormat = m_binReader.ReadUInt16();
numChannels = m_binReader.ReadUInt16();
sampleRate = m_binReader.ReadUInt32();
byteRate = m_binReader.ReadUInt32();
blockAssign = m_binReader.ReadUInt16();
BitsPerSample = m_binReader.ReadUInt16();
return true;
}
static void Main(string[] args)
{
p.Open("wavetest.wav");
bool a = p.ReadFmt();
p.ReadData();
}
}
}
发布的代码写得不是很好。
无论如何,您可以尝试以下快速更改:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace WaveTestRead
{
public class Program
{
public static void Main(string[] args)
{
using (var waveReader = new WaveReader())
{
if (!waveReader.Open("wavetest.wav"))
{
Console.WriteLine("Failed to read file.");
return;
}
if (!waveReader.ReadFmt())
{
Console.WriteLine("Failed to read fmt.");
return;
}
// this method is not defined...
//waveReader.ReadData();
}
}
}
class WaveReader : IDisposable
{
FileInfo m_fInfo;
FileStream m_fStream;
BinaryReader m_binReader;
// RIFF chunk
byte[] chunkID;
UInt32 chunkSize;
byte[] format;
// fmt subchunk
byte[] fmtChunkID;
UInt32 fmtChunkSize;
UInt16 audioFormat;
UInt16 numChannels;
UInt32 sampleRate;
UInt32 byteRate;
UInt16 blockAssign;
UInt16 BitsPerSample;
// data subchunk
byte[] dataChunkID;
UInt32 dataChunkSize;
byte[] data8L; // 8-bit left channel
byte[] data8R; // 8-bit right channel
Int16[] data16L; // 16-bit left channel
Int16[] data16R; // 16-bit right channel
int numSamples;
public WaveReader()
{
}
public bool Open(String filename)
{
string str;
m_fInfo = new FileInfo(filename);
m_fStream = m_fInfo.OpenRead();
m_binReader = new BinaryReader(m_fStream);
chunkID = new byte[4];
format = new byte[4];
chunkID = m_binReader.ReadBytes(4);
chunkSize = m_binReader.ReadUInt32();
format = m_binReader.ReadBytes(4);
str = System.Text.ASCIIEncoding.ASCII.GetString(chunkID, 0, 4);
if (str != "RIFF")
return false;
str = System.Text.ASCIIEncoding.ASCII.GetString(format, 0, 4);
if (str != "WAVE")
return false;
//if (ReadFmt() == false)
// return false;
//if (ReadData() == false)
// return false;
return true;
}
public bool ReadFmt()
{
fmtChunkID = new byte[4];
fmtChunkID = m_binReader.ReadBytes(4);
string str = System.Text.ASCIIEncoding.ASCII.GetString(fmtChunkID, 0, 4);
if (str != "fmt ")
return false;
fmtChunkSize = m_binReader.ReadUInt32();
audioFormat = m_binReader.ReadUInt16();
numChannels = m_binReader.ReadUInt16();
sampleRate = m_binReader.ReadUInt32();
byteRate = m_binReader.ReadUInt32();
blockAssign = m_binReader.ReadUInt16();
BitsPerSample = m_binReader.ReadUInt16();
return true;
}
public void Dispose()
{
if (m_fStream != null)
m_fStream.Dispose();
}
}
}
基本上,我已经用你的代码创建了一个类WaveReader,并删除了对ReadFmt的内部调用。然后在 Main 方法中,我检查了返回代码,如果为 false,我会写入控制台。
问题可能存在于Main
中。 Open
方法在最后关闭文件,Main 中的ReadFmt
调用从 m_binReader
读取。 要么在Open
调用后不要从 main 方法调用 ReadFmt
,要么将Open
更改为在完成后不关闭文件(并使关闭显式
看起来Open
无论如何都在为您完成所有工作(通过致电ReadFmt
和ReadData
)。 您无需在Main
中再次执行此操作;只需从p
访问数据即可。