Esempio n. 1
0
        /// <summary>
        /// 异步音量调节处理器
        /// </summary>
        /// <param name="tp">参数包装</param>
        private void FadeHandler(object tp)
        {
            Musician.GetInstance().IsBgmFading = true;
            var t        = tp as Tuple <float, float, int>;
            var beginVol = t.Item1;
            var destVol  = t.Item2;
            var fadeMs   = t.Item3;
            // 计算每次变更的时间间隔
            var deltaVol = destVol - beginVol;
            var dv       = deltaVol / fadeMs;

            dv = dv > 0 ? (float)Math.Max(0.0001, dv) : (float)Math.Min(-0.0001, dv);
            while (Math.Abs(this.Volume - destVol) > 0.025)
            {
                this.Volume += dv;
                Thread.Sleep(1);
            }
            this.Volume = destVol;
            Musician.GetInstance().IsBgmFading = false;
        }
        /// <summary>
        /// 音乐控制线程的入口
        /// </summary>
        private static void MusicianHandler()
        {
            var musician = Musician.GetInstance();

            while (true)
            {
                if (MusicianRouterHandler.TerminalFlag)
                {
                    MusicianRouterHandler.IsInitialized = false;
                    break;
                }
                MusicianMessage mmsg = null;
                lock (MusicianRouterHandler.syncObject)
                {
                    if (MusicianRouterHandler.messageQueue.Any())
                    {
                        mmsg = MusicianRouterHandler.messageQueue.Dequeue();
                    }
                }
                if (mmsg == null)
                {
                    Thread.Sleep(50);
                    continue;
                }
                switch (mmsg.Type)
                {
                case MusicianOperation.PlayBGM:
                    musician.PlayBGM(
                        mmsg.Args["resourceName"] as string,
                        mmsg.Args["ms"] as MemoryStream,
                        (float)mmsg.Args["vol"]
                        );
                    break;

                case MusicianOperation.PauseBGM:
                    musician.PauseBGM();
                    break;

                case MusicianOperation.ResumeBGM:
                    musician.ResumeBGM();
                    break;

                case MusicianOperation.StopAndReleaseBGM:
                    musician.StopAndReleaseBGM();
                    break;

                case MusicianOperation.PlayBGS:
                    musician.PlayBGS(
                        mmsg.Args["resourceName"] as string,
                        mmsg.Args["ms"] as MemoryStream,
                        (float)mmsg.Args["vol"],
                        (int)mmsg.Args["track"]
                        );
                    break;

                case MusicianOperation.StopBGS:
                    musician.StopBGS();
                    break;

                case MusicianOperation.SetBGMVolume:
                    musician.SetBGMVolume(
                        (float)mmsg.Args["vol"]
                        );
                    break;

                case MusicianOperation.SetBGSVolume:
                    musician.SetBGSVolume(
                        (float)mmsg.Args["vol"],
                        (int)mmsg.Args["track"]
                        );
                    break;

                case MusicianOperation.PlaySE:
                    musician.PlaySE(
                        mmsg.Args["ms"] as MemoryStream,
                        (float)mmsg.Args["vol"]
                        );
                    break;

                case MusicianOperation.PlayVocal:
                    musician.PlayVocal(
                        mmsg.Args["ms"] as MemoryStream,
                        (float)mmsg.Args["vol"]
                        );
                    break;

                case MusicianOperation.FadeBgm:
                    musician.FadeBgm(
                        (float)mmsg.Args["vol"],
                        (int)mmsg.Args["ms"]
                        );
                    break;

                case MusicianOperation.StopAndReleaseVocal:
                    musician.StopAndReleaseVocal();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            MusicianRouterHandler.IsCollapsed = true;
        }