播放声音时,我需要分别调整左右声道的音量。
我有上课演奏声音的方法:
public class SoundPlayer
{
private WaveOutEvent _outputDevice;
private AudioFileReader _audioFile;
private float _volume = 1f;
public float Volume
{
get => _volume;
set
{
_volume = value;
if (_audioFile != null)
_audioFile.Volume = value;
}
}
public void Play(string fileName)
{
if (_outputDevice == null)
{
_outputDevice = new WaveOutEvent();
_outputDevice.PlaybackStopped += (sender, args) =>
{
_outputDevice.Dispose();
_outputDevice = null;
_audioFile.Dispose();
_audioFile = null;
};
}
if (_audioFile == null)
{
_audioFile = new AudioFileReader(fileName) { Volume = _volume };
_outputDevice.Init(_audioFile);
}
else
{
if (string.IsNullOrWhiteSpace(fileName))
_outputDevice = null;
else
{
if (_audioFile.FileName != fileName)
{
_audioFile = new AudioFileReader(fileName) { Volume = _volume };
_outputDevice.Init(_audioFile);
}
}
}
_outputDevice?.Play();
}
public void Stop()
{
_outputDevice?.Stop();
}
}
但是在本类(class)中,您只能调整整体音量。如何做这样的属性(property):
soundPlayer.LeftChannelVolume = 1.0f
soundPlayer.RightChannelVolume = 0.5f
最佳答案
在PanningSampleProvider的帮助下制作的。但是为此,您必须转换为单声道。同样,对Pan-变化的反应会稍有延迟。如何避免这种情况?如何使用立体声并仅改变其左右声道的音量?我认为这应该更快。
_audioFile = new AudioFileReader(_fileName) { Volume = _volume };
var mono = new StereoToMonoSampleProvider(_audioFile) { LeftVolume = 1f, RightVolume = 1f };
var panner = new PanningSampleProvider(mono);
_outputDevice.Init(panner);
关于c# - 使用NAudio更改左右声道的声音平衡,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61251734/