C# NAudio体积变化
我真的不知道这里有什么问题,我很高兴能得到帮助,我正在尝试更改输出音频的音量。 编译此代码时,我在C# NAudio体积变化,c#,naudio,C#,Naudio,我真的不知道这里有什么问题,我很高兴能得到帮助,我正在尝试更改输出音频的音量。 编译此代码时,我在output.volume=0.5中得到一个错误。错误是: 设置DirectSoundOut上不支持的音量,请改为调整WaveProvider上的音量 这意味着,使用WaveChannel32上的Volume属性。另外,除非您使用的是旧版本的NAudio,否则块对齐还原流和波形转换流是不必要的,因为MP3FileReader发出PCM。+1表示新的NAudio.Wave.WaveChannel32(
output.volume=0.5
中得到一个错误。错误是:
设置DirectSoundOut上不支持的音量,请改为调整WaveProvider上的音量
这意味着,使用
WaveChannel32
上的Volume
属性。另外,除非您使用的是旧版本的NAudio,否则块对齐还原流
和波形转换流
是不必要的,因为MP3FileReader
发出PCM。+1表示新的NAudio.Wave.WaveChannel32(新的NAudio.Wave.WaveFileReader(文件名))
,它帮助我在使用Mp3FileReader时访问Volume
属性(Mp3FileReader本身不包含此属性)。
class Sound
{
private NAudio.Wave.BlockAlignReductionStream stream = null;
private NAudio.Wave.DirectSoundOut output = null;
private string fileName;
public Sound(string fileName)
{
this.fileName = fileName;
}
public void PlaySound()
{
if(fileName.EndsWith(".mp3"))
{
NAudio.Wave.WaveStream pcm = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.Mp3FileReader(fileName));
stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
}
else if (fileName.EndsWith(".wav"))
{
NAudio.Wave.WaveStream pcm = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(fileName));
stream = new NAudio.Wave.BlockAlignReductionStream(pcm);
}
else throw new InvalidOperationException("Not a correct audio file type.");
output = new NAudio.Wave.DirectSoundOut();
output.Init(stream);
output.Play();
output.Volume = 0.5f;
}
public void Volume(float vol)
{
}
public void PausePlay()
{
if (output != null)
{
if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Pause();
else if (output.PlaybackState == NAudio.Wave.PlaybackState.Paused) output.Play();
}
}
public void Pause()
{
if (output != null)
{
if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Pause();
}
}
public void Play()
{
if (output != null)
{
if (output.PlaybackState == NAudio.Wave.PlaybackState.Paused) output.Play();
}
}
public void DisposeWave()
{
if (output != null)
{
if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Stop();
output.Dispose();
output = null;
}
if (stream != null)
{
stream.Dispose();
stream = null;
}
}
public bool Over()
{
if (stream.Position == stream.Length)
return true;
return false;
}
public void Loop()
{
if (Over())
{
stream.Position = 0;
output.Play();
}
}