Search code examples
c#audiofadingcscore

How to use CSCore.Streams.FadeInOut


I want to use the class CSCore.Streams.FadeInOut, which is contained in the CSCore-Library. I want to fade in and fade out the songs I play. But I don't know how to use it. Does anybody know?


Solution

  • As you already mentioned, you can use the FadeInOut-class. Checkout this example:

        IWaveSource source = CodecFactory.Instance.GetCodec(@"C:\Temp\test.mp3");
        using (var fadeInOut = new FadeInOut(source, 1.0f))
        {
            using (var soundOut = new WasapiOut())
            {
                soundOut.Initialize(fadeInOut.ToWaveSource());
                soundOut.Play();
    
                Thread.Sleep(1000);
    
                fadeInOut.StartFading(2, 0.0f); //reduce the volume to 0.0f over 2 seconds
    
                Thread.Sleep(3000);
    
                fadeInOut.StopFading(); //stop fading
                fadeInOut.StartFading(2, 1.0f); //fade the volume to 1.0f over 2 seconds
    
                Console.ReadKey();
            }
        }