This project is read-only.

Null Reference Exception on playing the wave file in loop stream with repetitive play & Stop

Jun 17, 2015 at 2:48 PM
Hi,

I have a feature that plays the wave file in a loop, and user allowed to play and stop, 'n' number of times.

with reference to one of my post,
(http://stackoverflow.com/questions/30365365/change-wave-file-left-and-right-volumes-separately-in-c-sharp),
i have overridden SampleSourceBase class based on my requirement.
public class VPWaveSource : SampleSourceBase
{
        public VPWaveSource(ISampleSource source) : base(source)
        {
                if (source.WaveFormat.Channels != 2)
                     throw new ArgumentException("Source must have two channels.", "source");
        }

        public override int Read(float[] buffer, int offset, int count)
        {
              int samplesRead = base.Read(buffer, offset, count);
              for (int n = 0; n < samplesRead; n += 2)
              {
                    buffer[n] *= LeftVolume;
                    buffer[n + 1] *= RightVolume;
              }
             return samplesRead;
        }
}
Now i have used the SampleSourceBase class in my application as written below.
public class SoundManager
{
       private List<AudioEndpointVolumeChannel> lsChannels;
       WasapiOut _soundOut;
        public float vMinDB = 0.0f;
        public float vMaxDB = 0.0f; 
        public float vIncrDB = 0.0f;
   

        public SoundManager()
        {
              IWaveSource waveSource = CodecFactory.Instance.GetCodec("C:\\Sample.wav");
              _soundOut = new WasapiOut();
              vpWaveSource = new VPWaveSource(waveSource.ToSampleSource());

              // Setting the system Master volume to maximum
              AudioEndpointVolume aepv = AudioEndpointVolume.FromDevice(_soundOut.Device);
              aepv.GetVolumeRange(out vMinDB, out vMaxDB, out vIncrDB);
              aepv.SetMasterVolumeLevel(vMaxDB, Guid.Empty);
        }
        
        public void PlayNoiseFile()
        {
            if (_soundOut != null)
            {
                //vpWaveSource = new VPWaveSource(waveSource.ToSampleSource());
                LoopStream stream = new LoopStream(vpWaveSource.ToWaveSource());

                _soundOut.Initialize(stream);

                vpWaveSource.LeftVolume = 0.4f;
                vpWaveSource.RightVolume = 0.6f;

                // Intentionally playing through thread to work on UI components in parallel.
                playThread = new Thread(_soundOut.Play);
                playThread.Start();
            }
       
            public void StopNoiseFile()
            {
                if (_soundOut != null)
                {
                    playThread.Abort();
                    _soundOut.Stop();
                }
           }
        }
}
I have play and stop buttons. Clicking play invokes "PlayNoise" file and Stop invokes "StopNoiseFile" function.

On clicking the file play and stop button for more than 4 times, i am getting null reference exception at VPWaveSource.Read function (1st line - base.Read(buffer, offset, count)).

Can you please help me, where actually the problem raises.


Thanks in advance.


Regards,
Siva Rapolu