Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NAudio Volume Changing

Tags:

c#

naudio

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.

like image 676
user1139211 Avatar asked Jan 20 '12 18:01

user1139211


1 Answers

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.

like image 59
Mark Heath Avatar answered Sep 17 '22 18:09

Mark Heath