问题
Is there any way with NAudio to link a WaveMixerStream32 with WaveProviders, rather than WaveStreams? I am streaming multiple network streams, using a BufferedWaveProvider. There doesn't seem to be an easy way to convert it into a WaveStream.
Cheers!
Luke
回答1:
It's a fairly simple to convert an IWaveProvider to a WaveStream. An IWaveProvider is just a simplified WaveStream that doesn't support repositioning and has an unknown length. You can create an adapter like this:
public class WaveProviderToWaveStream : WaveStream
{
private readonly IWaveProvider source;
private long position;
public WaveProviderToWaveStream(IWaveProvider source)
{
this.source = source;
}
public override WaveFormat WaveFormat
{
get { return source.WaveFormat; }
}
/// <summary>
/// Don't know the real length of the source, just return a big number
/// </summary>
public override long Length
{
get { return Int32.MaxValue; }
}
public override long Position
{
get
{
// we'll just return the number of bytes read so far
return position;
}
set
{
// can't set position on the source
// n.b. could alternatively ignore this
throw new NotImplementedException();
}
}
public override int Read(byte[] buffer, int offset, int count)
{
int read = source.Read(buffer, offset, count);
position += read;
return read;
}
}
I've put some comments in about the Length and Position properties. What you need to do with them depends on whether the class you are passing this into attempts to make use of those properties or not.
Also, there is nothing stopping you creating your own version of WaveMixerStream32 that works on IWaveProvider. You could simplify things a lot since there would be no need to implement any repositioning logic in the mixer since you can't reposition any of your inputs.
来源:https://stackoverflow.com/questions/6636142/wavemixerstream32-and-iwaveprovider