我的程序应该播放我放入的.wav文件。它的作用是当我运行它时播放波形文件,然后接下来的几次我运行它不播放任何东西,然后它再次播放波形文件并重复此循环。基本上它是随机的,或者似乎,以太播放文件或不播放。我正在使用NAudio WaveStream播放它......
private static DirectSoundOut output = null;
private static BlockAlignReductionStream stream = null;
private static WaveFile song;
static void Main(string[] args)
{
AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit);
song = new WaveFile(filePath);
Wave wave = new Wave(song);
stream = new BlockAlignReductionStream(wave);
output = new DirectSoundOut();
output.Init(stream);
output.Play();
while (true)
{
}
}
static void OnProcessExit(object sender, EventArgs e)
{
if (output != null)
{
output.Stop();
output.Dispose();
output = null;
}
if(stream != null)
{
stream.Dispose();
stream = null;
}
}
Wave类......
class Wave : WaveStream
{
public WaveFile waveFile;
public int sampleRate;
public int bitDepth;
public int channels;
public byte[] byteArray;
public long sample = 0;
public Wave(int inputSampleRate, int inputBitDepth, int inputChannels, byte[] inputByteArray)
{
sampleRate = inputSampleRate;
bitDepth = inputBitDepth;
channels = inputChannels;
byteArray = (byte[])inputByteArray.Clone();
}
public Wave(WaveFile inputWaveFile)
{
waveFile = inputWaveFile;
sampleRate = waveFile.sampleRate;
bitDepth = waveFile.bitDepth;
channels = waveFile.channels;
byteArray = (byte[])waveFile.byteArray.Clone();
}
public override long Position
{
get;
set;
}
public override long Length
{
get { return long.MaxValue; }
}
public override WaveFormat WaveFormat
{
get { return new WaveFormat(sampleRate, bitDepth, channels); }
}
public override int Read(byte[] buffer, int offset, int numBytes)
{
for (int i = 0; i < numBytes; i++)
{
buffer[i] = byteArray[sample + i];
}
sample += numBytes;
return numBytes;
}
}
WaveFile类......
class WaveFile
{
public int chunkID;
public int chunkSize;
public int format;
public int fmtID;
public int fmtSize;
public int fmtCode;
public int channels;
public int sampleRate;
public int fmtAvgBPS;
public int fmtBlockAlign;
public int bitDepth;
public int fmtExtraSize;
public int dataSize;
public byte[] byteArray;
public WaveFile(String filePath)
{
Stream waveFileStream = new FileStream(filePath, FileMode.Open);
BinaryReader reader = new BinaryReader(waveFileStream);
Console.Write("Chunk Id: " + reader.ReadChar());
Console.Write(reader.ReadChar());
Console.Write(reader.ReadChar());
Console.WriteLine(reader.ReadChar());
Console.WriteLine("File is " + (chunkSize = reader.ReadInt32()) + " bytes long.");
Console.Write("Format: " + reader.ReadChar());
Console.Write(reader.ReadChar());
Console.Write(reader.ReadChar());
Console.WriteLine(reader.ReadChar());
Console.Write("SubChunk Id: " + reader.ReadChar());
Console.Write(reader.ReadChar());
Console.Write(reader.ReadChar());
Console.WriteLine(reader.ReadChar());
Console.WriteLine("SubChunk is " + (fmtSize = reader.ReadInt32()) + " bytes long.");
Console.WriteLine("Audio Format: " + (fmtCode = reader.ReadInt16()));
Console.WriteLine("channels: " + (channels = reader.ReadInt16()));
Console.WriteLine("SampleRate: " + (sampleRate = reader.ReadInt32()));
Console.WriteLine("Bytes for each second: " + (fmtAvgBPS = reader.ReadInt32()));
Console.WriteLine("Bytes for sample: " + (fmtBlockAlign = reader.ReadInt16()));
Console.WriteLine("BitDeapth: " + (bitDepth = reader.ReadInt16()));
Console.Write("SubChunk2 Id: " + reader.ReadChar());
Console.Write(reader.ReadChar());
Console.Write(reader.ReadChar());
Console.WriteLine(reader.ReadChar());
Console.WriteLine("Bytes for enitre audio data: " + (dataSize = reader.ReadInt32()));
if (fmtSize == 18)
{
// Read any extra values
fmtExtraSize = reader.ReadInt16();
reader.ReadBytes(fmtExtraSize);
}
byteArray = reader.ReadBytes(dataSize);
waveFileStream.Dispose();
reader.Dispose();
}
}
我的问题为什么这个代码有时会在我运行时播放音频。任何帮助表示赞赏。
谢谢。