星火微课系统客户端
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DeviceWindow.xaml.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. using Accord.Video;
  2. using Accord.Video.DirectShow;
  3. using Common.system;
  4. using NAudio.CoreAudioApi;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.Drawing.Imaging;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading;
  12. using System.Windows;
  13. using System.Windows.Input;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Threading;
  16. namespace XHWK.WKTool
  17. {
  18. /// <summary>
  19. /// DeviceWindow.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class DeviceWindow : Window
  22. {
  23. private bool cameraGood = false;
  24. private bool microphoneGood = false;
  25. private bool loudspeakerGood = false;
  26. public DeviceWindow()
  27. {
  28. InitializeComponent();
  29. cameraCheck();
  30. }
  31. /// <summary>
  32. /// 初始化
  33. /// </summary>
  34. public void Initialize()
  35. {
  36. TxbCamera.Text = "";
  37. TxbSpeaker.Text = "";
  38. TxbMicrophone.Text = "";
  39. }
  40. /// <summary>
  41. /// 窗体移动
  42. /// </summary>
  43. /// <param name="sender"></param>
  44. /// <param name="e"></param>
  45. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  46. {
  47. DragMove();
  48. }
  49. /// <summary>
  50. /// 关闭当前页
  51. /// </summary>
  52. /// <param name="sender"></param>
  53. /// <param name="e"></param>
  54. private void BtnDown_Click(object sender, RoutedEventArgs e)
  55. {
  56. imgPlayer.Visibility = Visibility.Hidden;
  57. Close();
  58. }
  59. #region 摄像头
  60. private void cameraCheck()
  61. {
  62. HidePage();
  63. GridCamera.Visibility = Visibility.Visible;
  64. var devs = new FilterInfoCollection(FilterCategory.VideoInputDevice);//获取摄像头列表
  65. CmbCameraList.ItemsSource = devs;
  66. if (devs.Count > 0)
  67. {
  68. CmbCameraList.SelectedIndex = 0;
  69. }
  70. }
  71. private void CmbCameraList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  72. {
  73. Console.WriteLine("选择变化");
  74. checkCamera();
  75. }
  76. private VideoCaptureDevice Camera;//用来操作摄像头
  77. /// <summary>
  78. /// 检测
  79. /// </summary>
  80. /// <param name="sender"></param>
  81. /// <param name="e"></param>
  82. private void checkCamera()
  83. {
  84. try
  85. {
  86. closeCamera();
  87. imgPlayer.Visibility = Visibility.Visible;
  88. if (CmbCameraList.SelectedIndex >= 0)
  89. {
  90. var info = CmbCameraList.SelectedItem as FilterInfo;
  91. Camera = new VideoCaptureDevice(info.MonikerString);
  92. //配置录像参数(宽,高,帧率,比特率等参数)VideoCapabilities这个属性会返回摄像头支持哪些配置
  93. Camera.VideoResolution = Camera.VideoCapabilities[0];
  94. Camera.NewFrame += Camera_NewFrame;//设置回调,aforge会不断从这个回调推出图像数据,SnapshotFrame也是有待比较
  95. Camera.Start();//打开摄像头
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. LogHelper.WriteErrLog("【摄像头检测】(DeviceWindow)摄像头不可用:" + ex.Message, ex);
  101. }
  102. }
  103. private void closeCamera()
  104. {
  105. try
  106. {
  107. if (Camera != null)
  108. {
  109. Camera.SignalToStop();
  110. }
  111. }
  112. catch (Exception)
  113. {
  114. }
  115. }
  116. /// <summary>
  117. /// 摄像头输出回调
  118. /// </summary>
  119. private void Camera_NewFrame(object sender, NewFrameEventArgs eventArgs)
  120. {
  121. Dispatcher.Invoke(new Action(
  122. () =>
  123. {
  124. MemoryStream ms = new MemoryStream();
  125. eventArgs.Frame.Save(ms, ImageFormat.Bmp);
  126. BitmapImage image = new BitmapImage();
  127. image.BeginInit();
  128. image.StreamSource = new MemoryStream(ms.GetBuffer());
  129. ms.Close();
  130. image.EndInit();
  131. imgPlayer.Source = image;
  132. }));//同步显示
  133. }
  134. /// <summary>
  135. /// 停止
  136. /// </summary>
  137. /// <param name="sender"></param>
  138. /// <param name="e"></param>
  139. private void BtnCameraStop_Click(object sender, RoutedEventArgs e)
  140. {
  141. if (Camera != null)
  142. {
  143. Camera.SignalToStop();
  144. }
  145. }
  146. #endregion 摄像头
  147. #region 扬声器
  148. /// <summary>
  149. /// 扬声器
  150. /// </summary>
  151. /// <param name="sender"></param>
  152. /// <param name="e"></param>
  153. private void Speaker_Check()
  154. {
  155. closeCamera();
  156. CmbCameraList.SelectedIndex = -1;
  157. HidePage();
  158. GridSpeaker.Visibility = Visibility.Visible;
  159. }
  160. /// <summary>
  161. /// 检测扬声器
  162. /// </summary>
  163. /// <param name="sender"></param>
  164. /// <param name="e"></param>
  165. private void BtnSpeakerDetection_Click(object sender, RoutedEventArgs e)
  166. {
  167. Console.WriteLine("播放");
  168. MediaAudio.Source = new Uri(FileToolsCommon.GetFileAbsolutePath("/Resources/audiotest.mp3"));
  169. MediaAudio.Position = TimeSpan.Zero;
  170. MediaAudio.Play();
  171. ImgAcousticWave.Visibility = Visibility.Visible;
  172. }
  173. #region 音频播放
  174. /// <summary>
  175. /// 音频播放 同步进度条和视频进度
  176. /// </summary>
  177. /// <param name="sender"></param>
  178. /// <param name="e"></param>
  179. private void MediaAudio_MediaOpened(object sender, RoutedEventArgs e)
  180. {
  181. Console.WriteLine("播放中。。。");
  182. }
  183. private void MediaAudio_MediaEnded(object sender, RoutedEventArgs e)
  184. {
  185. MediaAudio.Stop();
  186. MediaAudio.Position = TimeSpan.Zero;
  187. }
  188. #endregion 音频播放
  189. #endregion 扬声器
  190. #region 麦克风
  191. /// <summary>
  192. /// 麦克风
  193. /// </summary>
  194. /// <param name="sender"></param>
  195. /// <param name="e"></param>
  196. private void Microphone_Check()
  197. {
  198. MediaAudio.Stop();
  199. ImgAcousticWave.Visibility = Visibility.Collapsed;
  200. HidePage();
  201. GridMicrophone.Visibility = Visibility.Visible;
  202. ImgMicAcousticWave.Visibility = Visibility.Hidden;
  203. string AudioPath = FileToolsCommon.GetFileAbsolutePath("/temp/audio/");
  204. FileToolsCommon.DeleteDirectory(AudioPath);
  205. DataTable data = new DataTable();
  206. List<string> MicrophoneList = APP.FFmpeg.GetMicrophoneNameList();
  207. if (MicrophoneList.Count > 0)
  208. {
  209. data.Columns.Add("Value");
  210. data.Columns.Add("Key");
  211. for (int i = 0; i < MicrophoneList.Count; i++)
  212. {
  213. //创建一行
  214. DataRow row = data.NewRow();
  215. //将此行添加到table中
  216. data.Rows.Add(row);
  217. data.Rows[i]["Value"] = MicrophoneList[i];
  218. data.Rows[i]["Key"] = i.ToString();
  219. }
  220. CmbMicrophoneList.ItemsSource = data.DefaultView;
  221. int CameraIndex = 0;
  222. if (MicrophoneList.Exists(x => x == APP.MicrophoneName))
  223. {
  224. CameraIndex = MicrophoneList.FindIndex(x => x == APP.MicrophoneName);
  225. }
  226. CmbMicrophoneList.SelectedIndex = CameraIndex;
  227. }
  228. }
  229. /// <summary>
  230. /// 检测麦克风
  231. /// </summary>
  232. /// <param name="sender"></param>
  233. /// <param name="e"></param>
  234. private void BtnMicrophoneDetection_Click(object sender, RoutedEventArgs e)
  235. {
  236. ImgMicAcousticWave.Visibility = Visibility.Hidden;
  237. //GridMicImg.Visibility = Visibility.Hidden;
  238. string AudioPath = FileToolsCommon.GetFileAbsolutePath("/temp/audio/");
  239. FileToolsCommon.CreateDirectory(AudioPath);
  240. string audioMicrophonePath = AudioPath + "adoM" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".mp3";
  241. TbxMicrophoneDetection.Text = "录制中...";
  242. BtnMicrophoneDetection.Content = "正在录制";
  243. BtnMicrophoneDetection.IsEnabled = false;
  244. new Thread(new ThreadStart(new Action(() =>
  245. {
  246. #region 检测麦克风
  247. bool Record = APP.FFmpeg.StartRecordAudio(audioMicrophonePath, APP.MicrophoneName, false);
  248. for (int i = 5; i > 0; i--)
  249. {
  250. Dispatcher.Invoke(() =>
  251. {
  252. TbxMicrophoneDetection.Text = "请说话(" + i + "s)";
  253. });
  254. Thread.Sleep(1000);
  255. }
  256. if (Record)
  257. {
  258. if (FileToolsCommon.IsExistFile(audioMicrophonePath))
  259. {
  260. Dispatcher.Invoke(() =>
  261. {
  262. TbxMicrophoneDetection.Text = "录制成功";
  263. MediaAudioMicPlay.Source = new Uri(audioMicrophonePath);
  264. ImgMicAcousticWave.Visibility = Visibility.Visible;
  265. MediaAudioMicPlay.Play();
  266. BtnMicrophoneDetection.Content = "回响中";
  267. });
  268. }
  269. else
  270. {
  271. //无法录制麦克风
  272. Dispatcher.Invoke(() =>
  273. {
  274. TbxMicrophoneDetection.Text = "不可用";
  275. FileToolsCommon.SetConfigValue("IsRecordingMicrophone", "0");
  276. //GridMicImg.Visibility = Visibility.Hidden;
  277. });
  278. }
  279. }
  280. else
  281. {
  282. //无法录制麦克风
  283. Dispatcher.Invoke(() =>
  284. {
  285. TbxMicrophoneDetection.Text = "不可用";
  286. FileToolsCommon.SetConfigValue("IsRecordingMicrophone", "0");
  287. //GridMicImg.Visibility = Visibility.Hidden;
  288. });
  289. }
  290. APP.FFmpeg.StopRecordAudio(1);
  291. #endregion 检测麦克风
  292. Dispatcher.Invoke(() =>
  293. {
  294. BtnMicrophoneDetection.Content = "重新测试";
  295. BtnMicrophoneDetection.IsEnabled = true;
  296. });
  297. }))).Start();
  298. }
  299. /// <summary>
  300. /// 播放结束
  301. /// </summary>
  302. /// <param name="sender"></param>
  303. /// <param name="e"></param>
  304. private void MediaAudioMicPlay_MediaEnded(object sender, RoutedEventArgs e)
  305. {
  306. MediaAudioMicPlay.Stop();
  307. MediaAudioMicPlay.Position = TimeSpan.Zero;
  308. ImgMicAcousticWave.Visibility = Visibility.Hidden;
  309. BtnMicrophoneDetection.Content = "重新测试";
  310. BtnMicrophoneDetection.IsEnabled = true;
  311. }
  312. #endregion 麦克风
  313. #region 页面切换
  314. /// <summary>
  315. /// 检测页
  316. /// </summary>
  317. private void result_show()
  318. {
  319. HidePage();
  320. GridDetection.Visibility = Visibility.Visible;
  321. if (cameraGood)
  322. {
  323. TxbCamera.Text = "可用";
  324. }
  325. else
  326. {
  327. TxbCamera.Text = "不可用";
  328. }
  329. if (loudspeakerGood)
  330. {
  331. TxbSpeaker.Text = "可用";
  332. }
  333. else
  334. {
  335. TxbSpeaker.Text = "不可用";
  336. }
  337. if (microphoneGood)
  338. {
  339. TxbMicrophone.Text = "可用";
  340. }
  341. else
  342. {
  343. TxbMicrophone.Text = "不可用";
  344. }
  345. }
  346. /// <summary>
  347. /// 隐藏页
  348. /// </summary>
  349. private void HidePage()
  350. {
  351. GridDetection.Visibility = Visibility.Collapsed;
  352. GridCamera.Visibility = Visibility.Collapsed;
  353. GridSpeaker.Visibility = Visibility.Collapsed;
  354. GridMicrophone.Visibility = Visibility.Collapsed;
  355. try
  356. {
  357. CameraHelper.CloseDevice();
  358. }
  359. catch (Exception)
  360. {
  361. }
  362. }
  363. #endregion 页面切换
  364. #region 事件
  365. private void btn_camera_good_Click(object sender, RoutedEventArgs e)
  366. {
  367. cameraGood = true;
  368. Speaker_Check();
  369. }
  370. private void btn_camera_bad_Click(object sender, RoutedEventArgs e)
  371. {
  372. cameraGood = false;
  373. Speaker_Check();
  374. }
  375. private void btn_speaker_good_Click(object sender, RoutedEventArgs e)
  376. {
  377. loudspeakerGood = true;
  378. Microphone_Check();
  379. }
  380. private void btn_speaker_bad_Click(object sender, RoutedEventArgs e)
  381. {
  382. loudspeakerGood = false;
  383. Microphone_Check();
  384. }
  385. private void btn_microphone_good_Click(object sender, RoutedEventArgs e)
  386. {
  387. microphoneGood = true;
  388. result_show();
  389. }
  390. private void btn_microphone_bad_Click(object sender, RoutedEventArgs e)
  391. {
  392. microphoneGood = false;
  393. result_show();
  394. }
  395. /// <summary>
  396. /// 重新
  397. /// </summary>
  398. /// <param name="sender"></param>
  399. /// <param name="e"></param>
  400. private void BtnDetection_Click(object sender, RoutedEventArgs e)
  401. {
  402. cameraCheck();
  403. }
  404. #endregion 事件
  405. }
  406. }