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
{
///
/// DeviceWindow.xaml 的交互逻辑
///
public partial class DeviceWindow : Window
{
public DeviceWindow()
{
InitializeComponent();
}
///
/// 初始化
///
public void Initialize()
{
TxbCamera.Text = "";
TxbSpeaker.Text = "";
TxbMicrophone.Text = "";
BtnDetection.Content = "开始检测";
HidePage();
GridDetection.Visibility = Visibility.Visible;
}
///
/// 窗体移动
///
///
///
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
///
/// 关闭当前页
///
///
///
private void BtnDown_Click(object sender, RoutedEventArgs e)
{
imgPlayer.Visibility = Visibility.Hidden;
try
{
CameraHelper.CloseDevice();
}
catch (Exception)
{
}
Hide();
}
#region 设备检测
///
/// 开始检测
///
///
///
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();
}
///
/// 返回一个时间戳到毫秒
///
///
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 摄像头
///
/// 设置摄像头
///
///
///
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;
}
///
/// 检测
///
///
///
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);
}
}
///
/// 停止
///
///
///
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 扬声器
///
/// 检测扬声器
///
///
///
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 音频播放
/////
///// 计时器
/////
//DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); // 定义一个DT
///
/// 音频播放 同步进度条和视频进度
///
///
///
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启动
}
///
/// slider滑动值随播放内容位置变化
///
///
///
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 麦克风
///
/// 设置麦克风
///
///
///
private void BtnMicrophoneSave_Click(object sender, RoutedEventArgs e)
{
//CmbMicrophoneList.SelectedValue.ToString();
APP.MicrophoneName = CmbMicrophoneList.Text;
}
///
/// 加载麦克风列表
///
void lodingMicrophoneList()
{
//string name = APP.FFmpeg.GetMicrophoneName();
//List MicrophoneList = APP.FFmpeg.GetMicrophoneNameList();
}
///
/// 检测麦克风
///
///
///
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();
}
///
/// 播放录音
///
///
///
private void BtnMicrophoneDetectionPlay_Click(object sender, RoutedEventArgs e)
{
BtnMicrophoneDetectionPlay.Content = "播放中";
BtnMicrophoneDetectionPlay.IsEnabled = false;
ImgMicAcousticWave.Visibility = Visibility.Visible;
MediaAudioMicPlay.Play();
}
///
/// 播放结束
///
///
///
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 页面切换
///
/// 摄像头
///
///
///
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;
}
}
///
/// 扬声器
///
///
///
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间隔
}
///
/// 麦克风
///
///
///
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 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;
}
}
///
/// 检测页
///
///
///
private void BtnDetectionPage_Click(object sender, RoutedEventArgs e)
{
HidePage();
GridDetection.Visibility = Visibility.Visible;
}
///
/// 隐藏页
///
void HidePage()
{
GridDetection.Visibility = Visibility.Collapsed;
GridCamera.Visibility = Visibility.Collapsed;
GridSpeaker.Visibility = Visibility.Collapsed;
GridMicrophone.Visibility = Visibility.Collapsed;
try
{
CameraHelper.CloseDevice();
}
catch (Exception)
{
}
}
#endregion
}
}