ProjectorProcess.cs 3.3 KB
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.Common;
using TSA_V.DeviceLibrary;
using static TSA_V.Common.LogUtil;

namespace TSA_V
{
    public class ProjectorProcess
    {
        /// <summary>
        /// 第二屏幕是否显示,默认不显示
        /// </summary>
        public static bool ViewScreen_Show = false;

        public static bool AOIopen = false;

        public static bool DisShowScreenAlarm = ConfigAppSettings.GetBoolValue(Setting_Init.DisShowScreenAlarm);

        private static System.Timers.Timer proTimer     = null;

        private static bool InPlay = false;

        private static string soundFileName = @"\sound\alarm.wav";

        // 创建一个新的 SoundPlayer 实例
        private static SoundPlayer player = new SoundPlayer();
        public static  void Init()
        {
            if (proTimer == null)
            {
                proTimer = new System.Timers.Timer();
                proTimer.AutoReset = true;
                proTimer.Enabled = false;
                proTimer.Interval = 1000;
                proTimer.Elapsed += ProTimer_Elapsed; 
                proTimer.Start();
            } 
        }

        public static bool  IsAlarm(out string msg)
        {
            msg = "";
            if (!String.IsNullOrEmpty(TSAVBean.WarnMsg))
            {
                msg = TSAVBean.WarnMsg;
                return true;
            }
            else if(TSAVBean.NoAirAlarm)
            {
                msg =  ResourceControl.GetString(ResourceControl.NoAirAlarm, "未检测到气压信号"); ;
                return true;
            }
            return false;
        }

        private static  void ProTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            
            if (ProjectorProcess.IsAlarm(out string msg))
            {
                if (InPlay)
                {

                }
                else
                {
                    StartPlay();
                }
            }
            else
            {
                if (InPlay)
                {
                    StopPlay();
                }
            }
        }
    

        public static void StartPlay( )
        {

            // 音频文件路径
            string audioFilePath = Application.StartupPath+ soundFileName;
            LogUtil.info("开始播放:" + audioFilePath);

            try
            {
                if(File.Exists(audioFilePath))
                {
                    // 设置要播放的音频文件路径
                    player.SoundLocation = audioFilePath;
                    // 播放音频
                    player.PlayLooping();

                }
                else
                {
                    LogUtil.error("未找到音频文件:" + audioFilePath);
                }

                InPlay = true;
            }
            catch (Exception ex)
            {
                LogUtil.error("发生错误:" + ex.Message);
            }
             
        }


        public static void StopPlay()
        {
            InPlay = false;

            LogUtil.info("停止播放音频");
            // 停止播放音频
            player.Stop();

        }
    }
}