using Accord.Video;
using Accord.Video.DirectShow;
using Common.system;
using NAudio.CoreAudioApi;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
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
{
private bool cameraGood = false;
private bool microphoneGood = false;
private bool loudspeakerGood = false;
public DeviceWindow()
{
InitializeComponent();
cameraCheck();
}
///
/// 初始化
///
public void Initialize()
{
TxbCamera.Text = "";
TxbSpeaker.Text = "";
TxbMicrophone.Text = "";
}
///
/// 窗体移动
///
///
///
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
///
/// 关闭当前页
///
///
///
private void BtnDown_Click(object sender, RoutedEventArgs e)
{
imgPlayer.Visibility = Visibility.Hidden;
Close();
}
#region 摄像头
private void cameraCheck()
{
HidePage();
GridCamera.Visibility = Visibility.Visible;
var devs = new FilterInfoCollection(FilterCategory.VideoInputDevice);//获取摄像头列表
CmbCameraList.ItemsSource = devs;
if (devs.Count > 0)
{
CmbCameraList.SelectedIndex = 0;
}
}
private void CmbCameraList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
Console.WriteLine("选择变化");
checkCamera();
}
private VideoCaptureDevice Camera;//用来操作摄像头
///
/// 检测
///
///
///
private void checkCamera()
{
try
{
closeCamera();
imgPlayer.Visibility = Visibility.Visible;
if (CmbCameraList.SelectedIndex >= 0)
{
var info = CmbCameraList.SelectedItem as FilterInfo;
Camera = new VideoCaptureDevice(info.MonikerString);
//配置录像参数(宽,高,帧率,比特率等参数)VideoCapabilities这个属性会返回摄像头支持哪些配置
Camera.VideoResolution = Camera.VideoCapabilities[0];
Camera.NewFrame += Camera_NewFrame;//设置回调,aforge会不断从这个回调推出图像数据,SnapshotFrame也是有待比较
Camera.Start();//打开摄像头
}
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【摄像头检测】(DeviceWindow)摄像头不可用:" + ex.Message, ex);
}
}
private void closeCamera()
{
try
{
if (Camera != null)
{
Camera.SignalToStop();
}
}
catch (Exception)
{
}
}
///
/// 摄像头输出回调
///
private void Camera_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Dispatcher.Invoke(new Action(
() =>
{
MemoryStream ms = new MemoryStream();
eventArgs.Frame.Save(ms, ImageFormat.Bmp);
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = new MemoryStream(ms.GetBuffer());
ms.Close();
image.EndInit();
imgPlayer.Source = image;
}));//同步显示
}
///
/// 停止
///
///
///
private void BtnCameraStop_Click(object sender, RoutedEventArgs e)
{
if (Camera != null)
{
Camera.SignalToStop();
}
}
#endregion 摄像头
#region 扬声器
///
/// 扬声器
///
///
///
private void Speaker_Check()
{
closeCamera();
CmbCameraList.SelectedIndex = -1;
HidePage();
GridSpeaker.Visibility = Visibility.Visible;
}
///
/// 检测扬声器
///
///
///
private void BtnSpeakerDetection_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine("播放");
MediaAudio.Source = new Uri(FileToolsCommon.GetFileAbsolutePath("/Resources/audiotest.mp3"));
MediaAudio.Position = TimeSpan.Zero;
MediaAudio.Play();
ImgAcousticWave.Visibility = Visibility.Visible;
}
#region 音频播放
///
/// 音频播放 同步进度条和视频进度
///
///
///
private void MediaAudio_MediaOpened(object sender, RoutedEventArgs e)
{
Console.WriteLine("播放中。。。");
}
private void MediaAudio_MediaEnded(object sender, RoutedEventArgs e)
{
MediaAudio.Stop();
MediaAudio.Position = TimeSpan.Zero;
}
#endregion 音频播放
#endregion 扬声器
#region 麦克风
///
/// 麦克风
///
///
///
private void Microphone_Check()
{
MediaAudio.Stop();
ImgAcousticWave.Visibility = Visibility.Collapsed;
HidePage();
GridMicrophone.Visibility = Visibility.Visible;
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 BtnMicrophoneDetection_Click(object sender, RoutedEventArgs e)
{
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)
{
if (FileToolsCommon.IsExistFile(audioMicrophonePath))
{
Dispatcher.Invoke(() =>
{
TbxMicrophoneDetection.Text = "录制成功";
MediaAudioMicPlay.Source = new Uri(audioMicrophonePath);
ImgMicAcousticWave.Visibility = Visibility.Visible;
MediaAudioMicPlay.Play();
BtnMicrophoneDetection.Content = "回响中";
});
}
else
{
//无法录制麦克风
Dispatcher.Invoke(() =>
{
TbxMicrophoneDetection.Text = "不可用";
FileToolsCommon.SetConfigValue("IsRecordingMicrophone", "0");
//GridMicImg.Visibility = Visibility.Hidden;
});
}
}
else
{
//无法录制麦克风
Dispatcher.Invoke(() =>
{
TbxMicrophoneDetection.Text = "不可用";
FileToolsCommon.SetConfigValue("IsRecordingMicrophone", "0");
//GridMicImg.Visibility = Visibility.Hidden;
});
}
APP.FFmpeg.StopRecordAudio(1);
#endregion 检测麦克风
Dispatcher.Invoke(() =>
{
BtnMicrophoneDetection.Content = "重新测试";
BtnMicrophoneDetection.IsEnabled = true;
});
}))).Start();
}
///
/// 播放结束
///
///
///
private void MediaAudioMicPlay_MediaEnded(object sender, RoutedEventArgs e)
{
MediaAudioMicPlay.Stop();
MediaAudioMicPlay.Position = TimeSpan.Zero;
ImgMicAcousticWave.Visibility = Visibility.Hidden;
BtnMicrophoneDetection.Content = "重新测试";
BtnMicrophoneDetection.IsEnabled = true;
}
#endregion 麦克风
#region 页面切换
///
/// 检测页
///
private void result_show()
{
HidePage();
GridDetection.Visibility = Visibility.Visible;
if (cameraGood)
{
TxbCamera.Text = "可用";
}
else
{
TxbCamera.Text = "不可用";
}
if (loudspeakerGood)
{
TxbSpeaker.Text = "可用";
}
else
{
TxbSpeaker.Text = "不可用";
}
if (microphoneGood)
{
TxbMicrophone.Text = "可用";
}
else
{
TxbMicrophone.Text = "不可用";
}
}
///
/// 隐藏页
///
private void HidePage()
{
GridDetection.Visibility = Visibility.Collapsed;
GridCamera.Visibility = Visibility.Collapsed;
GridSpeaker.Visibility = Visibility.Collapsed;
GridMicrophone.Visibility = Visibility.Collapsed;
try
{
CameraHelper.CloseDevice();
}
catch (Exception)
{
}
}
#endregion 页面切换
#region 事件
private void btn_camera_good_Click(object sender, RoutedEventArgs e)
{
cameraGood = true;
Speaker_Check();
}
private void btn_camera_bad_Click(object sender, RoutedEventArgs e)
{
cameraGood = false;
Speaker_Check();
}
private void btn_speaker_good_Click(object sender, RoutedEventArgs e)
{
loudspeakerGood = true;
Microphone_Check();
}
private void btn_speaker_bad_Click(object sender, RoutedEventArgs e)
{
loudspeakerGood = false;
Microphone_Check();
}
private void btn_microphone_good_Click(object sender, RoutedEventArgs e)
{
microphoneGood = true;
result_show();
}
private void btn_microphone_bad_Click(object sender, RoutedEventArgs e)
{
microphoneGood = false;
result_show();
}
///
/// 重新
///
///
///
private void BtnDetection_Click(object sender, RoutedEventArgs e)
{
cameraCheck();
}
#endregion 事件
}
}