
using System;
using System.Collections.Generic;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;


namespace SlnMesnac.Common
{
    public class SpeechStr
    {
        private static readonly Lazy<SpeechStr> lazy = new Lazy<SpeechStr>(() => new SpeechStr());
        private readonly SpeechSynthesizer synthesizer;


        public static SpeechStr Instance
        {
            get
            {
                return lazy.Value;
            }
        }


        public SpeechStr()
        {
            synthesizer = new SpeechSynthesizer();
            // 可选：配置语音合成器
            synthesizer.Rate = 1; // 设置语速
            synthesizer.Volume = 100; // 设置音量
        }

        // 异步播放文本的方法
        public async Task SpeakAsync(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentException("Text cannot be null or empty.", nameof(text));
            }

            await Task.Run(() => synthesizer.SpeakAsync(text));
        }

        // 同步播放文本的方法
        public void Speak(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentException("Text cannot be null or empty.", nameof(text));
            }

            synthesizer.Speak(text);
        }

        public void StopSpeaking()
        {
            synthesizer.SpeakAsyncCancelAll();
        }
    }



}
