星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DeviceWindow.xaml.cs 13KB

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