NAudio 音量改变

发布于 2024-12-28 03:41:20 字数 2498 浏览 1 评论 0原文

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();

        }

    }

我真的不知道这里出了什么问题,我很高兴获得帮助,我正在尝试更改输出音频的音量。 当我编译此代码时,我在 output.volume = 0.5 中收到错误。错误是:

DirectSoundOut 不支持设置音量,请改为调整 WaveProvider 上的音量。

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();

        }

    }

I really don't know what's the problem here, I'd be glad for a help, I'm trying to change the volume of the output audio.
When I compile this code I'm getting an error in the output.volume = 0.5. The error is:

Setting volume not supported on DirectSoundOut, adjust the volume on your WaveProvider instead.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离旧人 2025-01-04 03:41:20

这意味着,请使用 WaveChannel32 上的 Volume 属性。另外,除非您使用旧版本的 NAudio,否则 BlockAlignReductionStreamWaveFormatConversion 流是不必要的,因为 MP3FileReader 会发出 PCM。

It means, use the Volume property on WaveChannel32 instead. Also, unless you are using an old version of NAudio, the BlockAlignReductionStream and the WaveFormatConversion stream are unneccessary, since MP3FileReader emits PCM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文