123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675 |
- using Common.system;
-
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Threading;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Media.Imaging;
-
- using System.Windows.Threading;
-
- namespace XHWK.WKTool
- {
- /// <summary>
- /// DeviceWindow.xaml 的交互逻辑
- /// </summary>
- public partial class DeviceWindow : Window
- {
- public DeviceWindow()
- {
- InitializeComponent();
- }
- /// <summary>
- /// 初始化
- /// </summary>
- public void Initialize()
- {
- TxbCamera.Text = "";
- TxbSpeaker.Text = "";
- TxbMicrophone.Text = "";
- BtnDetection.Content = "开始检测";
- HidePage();
- GridDetection.Visibility = Visibility.Visible;
- }
- /// <summary>
- /// 窗体移动
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- DragMove();
- }
- /// <summary>
- /// 关闭当前页
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnDown_Click(object sender, RoutedEventArgs e)
- {
- imgPlayer.Visibility = Visibility.Hidden;
- try
- {
- CameraHelper.CloseDevice();
- }
- catch (Exception)
- {
- }
- Hide();
- }
-
- #region 设备检测
- /// <summary>
- /// 开始检测
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnDetection_Click(object sender, RoutedEventArgs e)
- {
- string AudioPath = FileToolsCommon.GetFileAbsolutePath("/temp/audio/");
- FileToolsCommon.CreateDirectory(AudioPath);
- string audioSpeakerPath = AudioPath + "adoS" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".m";//FileToolsCommon.GetFileAbsolutePath("adoS.m");
- string audioMicrophonePath = AudioPath + "adoM" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".m";//FileToolsCommon.GetFileAbsolutePath("adoM.m");
- string PhotoPath = AudioPath + "Photo" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
- TxbCamera.Text = "检测中...";
- TxbSpeaker.Text = "检测中...";
- TxbMicrophone.Text = "检测中...";
- BtnDetection.Content = "正在检测";
- BtnDetection.IsEnabled = false;
- try
- {
- FileToolsCommon.DeleteFile(audioSpeakerPath);
- FileToolsCommon.DeleteFile(audioMicrophonePath);
- FileToolsCommon.DeleteFile(PhotoPath);
- }
- catch (Exception)
- {
-
- }
- new Thread(new ThreadStart(new Action(() =>
- {
- #region 检测摄像头
- try
- {
- //new Thread(new ThreadStart(new Action(() =>
- //{
- //Dispatcher.Invoke(() =>
- //{
- //});
- //}))).Start();
- Dispatcher.Invoke(() =>
- {
- CameraHelper.UpdateCameraDevices();
- });
- Thread.Sleep(500);
- if (CameraHelper.CameraDevices != null && CameraHelper.CameraDevices.Count > 0)
- {
- Dispatcher.Invoke(() =>
- {
- int CameraIndex = 0;
- for (int i = 0; i < CameraHelper.CameraDevices.Count; i++)
- {
- if (CameraHelper.CameraDevices[i].Name == "screen-capture-recorder")
- {
- continue;
- }
- if (CameraHelper.CameraDevices[i].MonikerString == APP.CameraName)
- {
- CameraIndex = i;
- }
- }
- CameraHelper.SetCameraDevice(CameraIndex);
- });
- Thread.Sleep(2000);
- //while()
- Dispatcher.Invoke(() =>
- {
- long time = Timestamp();
- string path = CameraHelper.CaptureImage(PhotoPath, time.ToString());
-
- if (string.IsNullOrWhiteSpace(path))
- {
- TxbCamera.Text = "不可用";
-
- }
- else
- {
- TxbCamera.Text = "正常";
- }
- CameraHelper.CloseDevice();
- });
- }
- else
- {
- Dispatcher.Invoke(() =>
- {
- TxbCamera.Text = "无设备";
- });
- }
- }
- catch (Exception)
- {
- Dispatcher.Invoke(() =>
- {
- TxbCamera.Text = "不可用";
- });
- }
- #endregion
- #region 检测扬声器
- if (APP.FFmpeg.StartRecordSpeakerAudio(audioSpeakerPath))
- {
- Dispatcher.Invoke(() =>
- {
- TxbSpeaker.Text = "正常";
- });
- }
- else
- {
- //无法录制扬声器音频
-
- Dispatcher.Invoke(() =>
- {
- TxbSpeaker.Text = "不可用";
- });
- }
- APP.FFmpeg.StopRecordAudio(2);
- #endregion
- Thread.Sleep(300);
- #region 检测麦克风
- if (APP.FFmpeg.StartRecordAudio(audioMicrophonePath, APP.MicrophoneName))
- {
- Dispatcher.Invoke(() =>
- {
- TxbMicrophone.Text = "正常";
- });
- }
- else
- {
- //无法录制麦克风
- Dispatcher.Invoke(() =>
- {
- TxbMicrophone.Text = "不可用";
- });
- }
- APP.FFmpeg.StopRecordAudio(1);
- #endregion
- Dispatcher.Invoke(() =>
- {
- BtnDetection.Content = "再次检测";
- BtnDetection.IsEnabled = true;
- });
- }))).Start();
- }
-
- /// <summary>
- /// 返回一个时间戳到毫秒
- /// </summary>
- /// <returns></returns>
- public static long Timestamp()
- {
- TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
- long timestr = Convert.ToInt64(ts.TotalMilliseconds);
- return timestr;
- }
- #endregion
-
- #region 摄像头
- /// <summary>
- /// 设置摄像头
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnCameraSave_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- for (int i = 0; i < CameraHelper.CameraDevices.Count; i++)
- {
- if (CameraHelper.CameraDevices[i].Name == "screen-capture-recorder")
- {
- continue;
- }
- if (CameraHelper.CameraDevices[i].Name == CmbCameraList.Text)
- {
- APP.CameraName = CameraHelper.CameraDevices[i].Name;
- }
- }
- if (times != null && times.Enabled == true)
- {
- times.Enabled = false;
- }
- imgPlayer.Visibility = Visibility.Hidden;
- try
- {
- CameraHelper.CloseDevice();
- }
- catch (Exception)
- {
- }
- I = 9999;
- MessageWindow.Show("设置成功!");
- }
- catch (Exception ex)
- {
- APP.CameraName = "";
- MessageWindow.Show("当前无法设置摄像头!");
- LogHelper.WriteErrLog("【摄像头检测】(DeviceWindow)当前无法设置摄像头:" + ex.Message, ex);
- }
- //APP.CameraListIndex = CmbCameraList.SelectedIndex;
- }
- /// <summary>
- /// 检测
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnCameraStat_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- imgPlayer.Visibility = Visibility.Visible;
- CameraHelper.UpdateCameraDevices();
- if (CameraHelper.CameraDevices.Count > 0)
- {
- int CameraIndex = 0;
- for (int i = 0; i < CameraHelper.CameraDevices.Count; i++)
- {
- if (CameraHelper.CameraDevices[i].Name == "screen-capture-recorder")
- {
- continue;
- }
- if (CameraHelper.CameraDevices[i].MonikerString == APP.CameraName)
- {
- CameraIndex = i;
- }
- }
- CameraHelper.SetCameraDevice(CameraIndex);
- string imgPath = FileToolsCommon.GetFileAbsolutePath("Temp/imgplayertest");
- FileToolsCommon.CreateDirectory(imgPath);
- times = new System.Timers.Timer(200)//初始值为300
- {
- AutoReset = true//设置是否执行System.Timers.Timer.Elapsed事件
- };//设置执行一次(false)还是一直执行(true)
- times.Elapsed += new System.Timers.ElapsedEventHandler(Times_Elapsed);
- times.Enabled = true; //启动计时器
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteErrLog("【摄像头检测】(DeviceWindow)摄像头不可用:" + ex.Message, ex);
- }
- }
-
- /// <summary>
- /// 停止
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnCameraStop_Click(object sender, RoutedEventArgs e)
- {
- times.Enabled = false;
- imgPlayer.Visibility = Visibility.Hidden;
- try
- {
- CameraHelper.CloseDevice();
- }
- catch (Exception ex)
- {
- LogHelper.WriteErrLog("【摄像头检测】(DeviceWindow)摄像头不可用:" + ex.Message, ex);
- }
- I = 9999;
- }
- #region 检测摄像头
-
- private System.Timers.Timer times;
-
- private int I = 9999;
- private void Times_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
- {
- try
- {
- Dispatcher.Invoke(() =>
- {
- if (I > 10010)
- {
- long time = Timestamp();
- string imgPath = FileToolsCommon.GetFileAbsolutePath("Temp/imgplayertest");
- //string FilePathName = ImgPath + RsImgName.Count + ".png";
- string path = CameraHelper.CaptureImage(imgPath, time.ToString());
- if (!string.IsNullOrWhiteSpace(path))
- {
- imgPlayer.Source = new BitmapImage(new Uri(path));
- }
- }
- I++;
- });
- }
- catch (Exception ex)
- {
- LogHelper.WriteErrLog("【摄像】(Times_Elapsed)生成图片错误:" + ex.Message, ex);
- }
- }
- #endregion
- #endregion
-
- #region 扬声器
- /// <summary>
- /// 检测扬声器
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnSpeakerDetection_Click(object sender, RoutedEventArgs e)
- {
- string AudioPath = FileToolsCommon.GetFileAbsolutePath("/temp/audio/");
- FileToolsCommon.CreateDirectory(AudioPath);
- string audioSpeakerPath = AudioPath + "adoS" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".m";
- TbxSpeakerDetection.Text = "检测中...";
- BtnSpeakerDetection.Content = "正在检测";
- BtnSpeakerDetection.IsEnabled = false;
- new Thread(new ThreadStart(new Action(() =>
- {
- Dispatcher.Invoke(() =>
- {
- MediaAudio.Play();
- ImgAcousticWave.Visibility = Visibility.Visible;
- });
- Thread.Sleep(4000);
- #region 检测扬声器
- if (APP.FFmpeg.StartRecordSpeakerAudio(audioSpeakerPath))
- {
- Dispatcher.Invoke(() =>
- {
- TbxSpeakerDetection.Text = "正常";
- });
- }
- else
- {
- //无法录制扬声器音频
- Dispatcher.Invoke(() =>
- {
- TbxSpeakerDetection.Text = "不可用";
- });
- }
- APP.FFmpeg.StopRecordAudio(2);
- #endregion
-
- Dispatcher.Invoke(() =>
- {
- ImgAcousticWave.Visibility = Visibility.Hidden;
- BtnSpeakerDetection.Content = "再次检测";
- BtnSpeakerDetection.IsEnabled = true;
- });
- }))).Start();
-
- }
- #region 音频播放
- ///// <summary>
- ///// 计时器
- ///// </summary>
- //DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); // 定义一个DT
- /// <summary>
- /// 音频播放 同步进度条和视频进度
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void MediaAudio_MediaOpened(object sender, RoutedEventArgs e)
- {
- //timelineSlider.Maximum = MediaAudio.NaturalDuration.TimeSpan.TotalMilliseconds; //设置slider最大值
- //int sec = (int)MediaAudio.NaturalDuration.TimeSpan.TotalSeconds;
- //dispatcherTimer.Start(); //DT启动
- }
- /// <summary>
- /// slider滑动值随播放内容位置变化
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dispatcherTimer_Tick(object sender, EventArgs e)
- {
- //timelineSlider.Value = MediaAudio.Position.TotalMilliseconds; //slider滑动值随播放内容位置变化
- }
- private void MediaAudio_MediaEnded(object sender, RoutedEventArgs e)
- {
- //dispatcherTimer.Stop();
- //timelineSlider.Value = 0;
- MediaAudio.Stop();
- MediaAudio.Position = TimeSpan.Zero;
- }
- #endregion
- #endregion
-
- #region 麦克风
- /// <summary>
- /// 设置麦克风
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnMicrophoneSave_Click(object sender, RoutedEventArgs e)
- {
- //CmbMicrophoneList.SelectedValue.ToString();
- APP.MicrophoneName = CmbMicrophoneList.Text;
- }
- /// <summary>
- /// 加载麦克风列表
- /// </summary>
- void lodingMicrophoneList()
- {
- //string name = APP.FFmpeg.GetMicrophoneName();
- //List<string> MicrophoneList = APP.FFmpeg.GetMicrophoneNameList();
- }
- /// <summary>
- /// 检测麦克风
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnMicrophoneDetection_Click(object sender, RoutedEventArgs e)
- {
- BtnMicrophoneDetectionPlay.Visibility = Visibility.Hidden;
- ImgMicAcousticWave.Visibility = Visibility.Hidden;
- //GridMicImg.Visibility = Visibility.Hidden;
- string AudioPath = FileToolsCommon.GetFileAbsolutePath("/temp/audio/");
- FileToolsCommon.CreateDirectory(AudioPath);
- string audioMicrophonePath = AudioPath + "adoM" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".mp3";
- TbxMicrophoneDetection.Text = "录制中...";
- BtnMicrophoneDetection.Content = "正在录制";
- BtnMicrophoneDetection.IsEnabled = false;
- new Thread(new ThreadStart(new Action(() =>
- {
- #region 检测麦克风
- bool Record = APP.FFmpeg.StartRecordAudio(audioMicrophonePath, APP.MicrophoneName, false);
- for (int i = 5; i > 0; i--)
- {
- Dispatcher.Invoke(() =>
- {
- TbxMicrophoneDetection.Text = "录制中..." + i + "s";
- });
- Thread.Sleep(1000);
- }
- if (Record)
- {
- Dispatcher.Invoke(() =>
- {
- TbxMicrophoneDetection.Text = "录制成功";
- BtnMicrophoneDetectionPlay.Visibility = Visibility.Visible;
- //GridMicImg.Visibility = Visibility.Visible;
- MediaAudioMicPlay.Source = new Uri(audioMicrophonePath);
- });
- }
- else
- {
- //无法录制麦克风
- Dispatcher.Invoke(() =>
- {
- TbxMicrophoneDetection.Text = "不可用";
- BtnMicrophoneDetectionPlay.Visibility = Visibility.Hidden;
- //GridMicImg.Visibility = Visibility.Hidden;
- });
- }
- APP.FFmpeg.StopRecordAudio(1);
- #endregion
- Dispatcher.Invoke(() =>
- {
- BtnMicrophoneDetection.Content = "重新录制";
- BtnMicrophoneDetection.IsEnabled = true;
- });
- }))).Start();
- }
- /// <summary>
- /// 播放录音
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnMicrophoneDetectionPlay_Click(object sender, RoutedEventArgs e)
- {
- BtnMicrophoneDetectionPlay.Content = "播放中";
- BtnMicrophoneDetectionPlay.IsEnabled = false;
- ImgMicAcousticWave.Visibility = Visibility.Visible;
- MediaAudioMicPlay.Play();
- }
- /// <summary>
- /// 播放结束
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void MediaAudioMicPlay_MediaEnded(object sender, RoutedEventArgs e)
- {
- MediaAudioMicPlay.Stop();
- MediaAudioMicPlay.Position = TimeSpan.Zero;
- BtnMicrophoneDetectionPlay.Content = "播放";
- BtnMicrophoneDetectionPlay.IsEnabled = true;
- ImgMicAcousticWave.Visibility = Visibility.Hidden;
- }
- #endregion
-
- #region 页面切换
- /// <summary>
- /// 摄像头
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnCamera_Click(object sender, RoutedEventArgs e)
- {
- HidePage();
- GridCamera.Visibility = Visibility.Visible;
-
- DataTable data = new DataTable();
- CameraHelper.UpdateCameraDevices();
- if (CameraHelper.CameraDevices.Count > 0)
- {
- data.Columns.Add("Value");
- data.Columns.Add("Key");
- for (int i = 0; i < CameraHelper.CameraDevices.Count; i++)
- {
- if (CameraHelper.CameraDevices[i].Name == "screen-capture-recorder")
- {
- continue;
- }
- //创建一行
- DataRow row = data.NewRow();
- //将此行添加到table中
- data.Rows.Add(row);
- data.Rows[i]["Value"] = CameraHelper.CameraDevices[i].Name;
- data.Rows[i]["Key"] = i.ToString();
- }
- CmbCameraList.ItemsSource = data.DefaultView;
- int CameraIndex = 0;
- for (int i = 0; i < CameraHelper.CameraDevices.Count; i++)
- {
- if (CameraHelper.CameraDevices[i].Name == "screen-capture-recorder")
- {
- continue;
- }
- if (CameraHelper.CameraDevices[i].MonikerString == APP.CameraName)
- {
- CameraIndex = i;
- }
- }
- CmbCameraList.SelectedIndex = CameraIndex;
- }
-
- }
- /// <summary>
- /// 扬声器
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnSpeaker_Click(object sender, RoutedEventArgs e)
- {
- HidePage();
- GridSpeaker.Visibility = Visibility.Visible;
-
- MediaAudio.Source = new Uri(FileToolsCommon.GetFileAbsolutePath("/Resources/AudioFrequency.mp3"));
- //dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); //超过计时间隔时发生
- //dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 500); //DT间隔
-
- }
- /// <summary>
- /// 麦克风
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnMicrophone_Click(object sender, RoutedEventArgs e)
- {
- HidePage();
- GridMicrophone.Visibility = Visibility.Visible;
- BtnMicrophoneDetectionPlay.Visibility = Visibility.Hidden;
- ImgMicAcousticWave.Visibility = Visibility.Hidden;
- string AudioPath = FileToolsCommon.GetFileAbsolutePath("/temp/audio/");
- FileToolsCommon.DeleteDirectory(AudioPath);
-
- DataTable data = new DataTable();
- List<string> MicrophoneList = APP.FFmpeg.GetMicrophoneNameList();
- if (MicrophoneList.Count > 0)
- {
- data.Columns.Add("Value");
- data.Columns.Add("Key");
- for (int i = 0; i < MicrophoneList.Count; i++)
- {
- //创建一行
- DataRow row = data.NewRow();
- //将此行添加到table中
- data.Rows.Add(row);
- data.Rows[i]["Value"] = MicrophoneList[i];
- data.Rows[i]["Key"] = i.ToString();
- }
- CmbMicrophoneList.ItemsSource = data.DefaultView;
- int CameraIndex = 0;
- if (MicrophoneList.Exists(x => x == APP.MicrophoneName))
- {
- CameraIndex = MicrophoneList.FindIndex(x => x == APP.MicrophoneName);
- }
- CmbMicrophoneList.SelectedIndex = CameraIndex;
- }
- }
- /// <summary>
- /// 检测页
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void BtnDetectionPage_Click(object sender, RoutedEventArgs e)
- {
- HidePage();
- GridDetection.Visibility = Visibility.Visible;
- }
- /// <summary>
- /// 隐藏页
- /// </summary>
- void HidePage()
- {
- GridDetection.Visibility = Visibility.Collapsed;
- GridCamera.Visibility = Visibility.Collapsed;
- GridSpeaker.Visibility = Visibility.Collapsed;
- GridMicrophone.Visibility = Visibility.Collapsed;
- try
- {
- CameraHelper.CloseDevice();
- }
- catch (Exception)
- {
- }
- }
- #endregion
-
-
- }
- }
|