NAudio icon indicating copy to clipboard operation
NAudio copied to clipboard

Added CachedSound constructor for playing resource wav files

Open YJ15 opened this issue 5 years ago • 4 comments

Added CachedSound constructor for playing resource wav files I played the wav file of Properties.Resource using the following source code:

public CachedSound(Stream sound) 
{
        using (var audioFileReader = new WaveFileReader(sound))
        {
            WaveFormat = audioFileReader.WaveFormat;
            var sp = audioFileReader.ToSampleProvider();
            var wholeFile = new List<float>((int)(audioFileReader.Length / 4));
            var sourceSamples = (int)(audioFileReader.Length / (audioFileReader.WaveFormat.BitsPerSample / 8));
            var sampleData = new float[sourceSamples];
            int samplesread;
            while ((samplesread = sp.Read(sampleData, 0, sourceSamples)) > 0)
            {
                wholeFile.AddRange(sampleData.Take(samplesread));
            }
            AudioData = wholeFile.ToArray();                
        }
}

YJ15 avatar Feb 28 '20 07:02 YJ15

"using System.IO;" Is missing. I fixed this error.

YJ15 avatar Feb 28 '20 08:02 YJ15

I think it's not good write to a list, it's better to write to a buffer(array) with some offset.

sampletext32 avatar Nov 13 '20 00:11 sampletext32

I think it's not good write to a list, it's better to write to a buffer(array) with some offset.

I create it based on public CachedSound(string audiofilename) in CachedSound.cs. Since lists are used in the source code, I also used lists. Why are arrays better than lists?

YJ15 avatar Oct 29 '21 11:10 YJ15

I needed this change as well, so I adapted it for the new layout in pull request #963.

jcummings2 avatar Nov 16 '22 18:11 jcummings2