using CommunityToolkit.Mvvm.Messaging; using DotNet4.Utilities; using HandyControl.Controls; using LiveTools.Data; using LiveTools.RuleItem; using Microsoft.Win32; using Newtonsoft.Json.Linq; using ryCommon; using ryCommonDb; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace LiveTools { /// /// Rule_SoundPlay.xaml 的交互逻辑 /// public partial class Rule_SoundPlay : UserControl, IRule { public Rule_SoundPlay() { InitializeComponent(); } public string RuleID { get;} = "SoundPlay"; public int ID { get; set; } = 0; public void LoadSetting(JObject jo) { TxtCustomSound.Text = jo.GetJsonValue("PlayCustomSound_Path", ""); ChkMultiPlaySound.IsChecked = jo.GetJsonValue("MultiPlaySound_On", false); NumPlaySoundInterval.Value = jo.GetJsonValue("PlaySoundInterval", 10); SliVol.Value = jo.GetJsonValue("Vol", 100); NumPlaySoundInterval.IsEnabled = ChkMultiPlaySound.IsChecked == false; } public JObject SettingJson() { return new JObject() { { "Name",this.GetType().Name}, { "Mode",RuleID}, { "PlayCustomSound_Path", TxtCustomSound.Text}, { "MultiPlaySound_On",ChkMultiPlaySound.IsChecked }, { "PlaySoundInterval", NumPlaySoundInterval.Value.ToInt() }, { "Vol", SliVol.Value.ToInt() } }; } public void Run(EffectInfo info, JObject jo) { //for (int m = 0; m < info.LoopCount; m++) //{ // for (int i = 0; i < info.Count; i++) // { // Random rd2 = new Random(Guid.NewGuid().GetHashCode()); // this.Dispatcher.Invoke(new Action(() => // { // Image simpleImage = new Image(); // simpleImage.Width = info.PicSize; // //Canvas.SetLeft(simpleImage, i * 300); // // Set the image source. // simpleImage.Source = GetImage(info.PicPath); // cvsGround.Children.Add(simpleImage); // RotateTransform rotateTransform = new RotateTransform(rd2.Next(0, 181)); // simpleImage.RenderTransform = rotateTransform; // KK(simpleImage); // })); // } //} } public bool CheckVerification() { if(!RyFiles.FileOrDirExist(API.GetTruePath(TxtCustomSound.Text))) { HandyControl.Controls.MessageBox.Warning("请选择音频文件或文件夹。", "提示"); return false; } return true; } private void UserControl_Loaded(object sender, RoutedEventArgs e) { } private void BtnClose_Click(object sender, RoutedEventArgs e) { WeakReferenceMessenger.Default.Send(new RuleMsg() { ID = ID, Message="Close" }); } private void BtnBrowserCustomSound_Click(object sender, RoutedEventArgs e) { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.FileName = API.GetTruePath(TxtCustomSound.Text); fileDialog.Filter = "音频文件|*.mp3;*.wav;*.flac;*.wma"; if (fileDialog.ShowDialog() == true) { TxtCustomSound.Text = API.GetRelativePath(fileDialog.FileName); } } private void BtnBrowserCustomSoundByFolder_Click(object sender, RoutedEventArgs e) { var fileDialog = new System.Windows.Forms.FolderBrowserDialog(); fileDialog.SelectedPath = API.GetTruePath(TxtCustomSound.Text); if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { TxtCustomSound.Text = API.GetRelativePath(fileDialog.SelectedPath); } } private void ChkMultiPlaySound_Checked(object sender, RoutedEventArgs e) { if (NumPlaySoundInterval == null) { return; } NumPlaySoundInterval.IsEnabled = ChkMultiPlaySound.IsChecked==false; } private void ChkMultiPlaySound_Unchecked(object sender, RoutedEventArgs e) { NumPlaySoundInterval.IsEnabled = ChkMultiPlaySound.IsChecked == false; } LiveTools.SoundPlay soundPlay = new LiveTools.SoundPlay(); private bool Playing { get; set; } = false; private void BtnPlay_Click(object sender, RoutedEventArgs e) { if (Playing) { soundPlay.Stop(); Playing = false;BtnPlay.Content = "播放"; return; } Playing = true; BtnPlay.Content = "停止"; var customSound_Path = API.GetTruePath(TxtCustomSound.Text); soundPlay.Volume = SliVol.Value.ToInt()/100f; new Thread(Start).Start(); void Start() { if (System.IO.File.Exists(customSound_Path)) { soundPlay.Play(customSound_Path); } else if (System.IO.Directory.Exists(customSound_Path)) { var files = RyFiles.GetFiles(customSound_Path,"*.mp3;*.wav;*.flac;*.wma"); if (files != null && files.Count>0) { Random rd = new Random(Guid.NewGuid().GetHashCode()); soundPlay.Play(files[rd.Next(0,files.Count)]); } } Dispatcher.Invoke(new Action(() => { BtnPlay.Content = "播放"; })); Playing = false; } } private void SliVol_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) { soundPlay.Volume = SliVol.Value.ToInt()/100f; } private void UserControl_Unloaded(object sender, RoutedEventArgs e) { if (Playing) { soundPlay.Stop(); Playing = false; } } } }