Ejemplo n.º 1
0
        /// <summary>
        /// Fades in the audio
        /// </summary>
        /// <param name="time">Time over which to fade the audio in</param>
        /// <param name="maxVolume">The maximum volume to reach</param>
        /// <param name="dVolume">The volume step size to use</param>
        /// <param name="fadeEndAction">What action to do at the end of the fade</param>
        public Task FadeIn(float time, float maxVolume, float dVolume, FadeEndAction fadeEndAction)
        {
            if (maxVolume > MaxVolume)
            {
                throw new ArgumentOutOfRangeException("The maximum fade in volume " + maxVolume + " was greater than the maximum volume for this audio " + MaxVolume);
            }
            var ct = _tokenSource.Token;

            return(Task.Run(async() =>
            {
                int dt = (int)(time / ((maxVolume - Volume) / dVolume));
                while (Volume < maxVolume && !ct.IsCancellationRequested)
                {
                    Volume += dVolume;
                    await Task.Delay(dt, ct);
                }
                switch (fadeEndAction)
                {
                case FadeEndAction.Pause:
                    Pause();
                    break;

                case FadeEndAction.Stop:
                    Stop();
                    break;
                }
            }, ct));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Fades out the audio.
        /// </summary>
        /// <param name="time"></param>
        /// <param name="minVolume"></param>
        /// <param name="dVolume"></param>
        /// <param name="fadeEndAction">What action to do at the end of the fade</param>
        public Task FadeOut(float time, float minVolume, float dVolume, FadeEndAction fadeEndAction)
        {
            var ct = _tokenSource.Token;

            return(Task.Run(async() =>
            {
                if (this.IsStopped)
                {
                    return;
                }
                int dt = (int)(time / ((Volume - minVolume) / dVolume));
                while (Volume > minVolume && !ct.IsCancellationRequested)
                {
                    Volume -= dVolume;
                    await Task.Delay(dt, ct);
                }
                switch (fadeEndAction)
                {
                case FadeEndAction.Pause:
                    Pause();
                    break;

                case FadeEndAction.Stop:
                    Stop();
                    break;
                }
            }, ct));
        }