SoundsController.cs 3.5 KB
using OnlineStore.Common;
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;

namespace DeviceLibrary
{
    /// <summary>
    /// 声音控制类
    /// </summary>
    public class SoundsController
    {

        private  static string openDoorFile = @"\sounds\doorOpen.wav";
        private static string closeDoorFile = @"\sounds\doorClose.wav";
        private static string string_Filling = @"\sounds\string_filling.wav";

        private static SoundPlayer alarmPlayer = new SoundPlayer();
        private static bool InPlay = false;
        private static string currFile = "";

        public static void StartOpen()
        {
            StartPlay(openDoorFile);

        }
        public static void StartPlayBack()
        {
            LogUtil.info($"开始播放:{string_Filling},{File.Exists(Application.StartupPath + string_Filling)}");
            StartPlay(string_Filling);
        }
        public static void StartClose() {
            StartPlay(closeDoorFile);
        }

        private static void StartPlay(string fileName)
        {
            if (!Setting_Init.Enable_DoorSound)
            {
                return;
            }
            if (InPlay)
            {
                if (currFile == fileName)
                {
                    return;
                }
                else
                {
                    StopPlay();
                }
            }
            // 音频文件路径
            string audioFilePath = Application.StartupPath + fileName;
            currFile = fileName;
            LogUtil.info("开始播放:" + currFile);

            try
            {
                if (File.Exists(audioFilePath))
                {
                    // 设置要播放的音频文件路径
                    alarmPlayer.SoundLocation = audioFilePath;
                    // 播放音频
                    alarmPlayer.PlayLooping();
                     
                }
                else
                {
                    LogUtil.error("未找到音频文件:" + audioFilePath);
                }

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


        public  static void StopPlay()
        {
            InPlay = false; 
            LogUtil.info("停止播放音频:"+currFile);
            // 停止播放音频
            alarmPlayer.Stop(); 
        }


        public static void PlayFile(string filename)
        {
            if (!Setting_Init.Enable_DoorSound)
            {
                return;
            }
            if (InPlay)
            {
                //报警中不播放。
                return;
            }

            // 音频文件路径
            string audioFilePath = Application.StartupPath + filename; 
            try
            {
                if (File.Exists(audioFilePath))
                {
                    // 设置要播放的音频文件路径
                    alarmPlayer.SoundLocation = audioFilePath;
                    // 播放音频
                    alarmPlayer.Play();
                }
                else
                {
                    LogUtil.error("未找到音频文件:" + audioFilePath);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("发生错误:" + ex.Message);
            }
        }
    }
}