星火微课系统客户端
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

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