diff --git a/Correlator/Dialog/SimplyAuditionDialog.xaml b/Correlator/Dialog/SimplyAuditionDialog.xaml index 8c1f422..5d38906 100644 --- a/Correlator/Dialog/SimplyAuditionDialog.xaml +++ b/Correlator/Dialog/SimplyAuditionDialog.xaml @@ -2,6 +2,7 @@ x:Class="Correlator.Dialog.SimplyAuditionDialog" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:b="http://schemas.microsoft.com/xaml/behaviors" xmlns:cvt="clr-namespace:Correlator.Converter" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" @@ -32,19 +33,19 @@ - + + - @@ -73,39 +74,14 @@ HorizontalAlignment="Stretch" Command="{Binding ListenRedSensorCommand}" Content="听红色传感器" - FontSize="14" /> + FontSize="14" + Style="{StaticResource FunctionButtonStyle}" /> - - - - - - - - - - - @@ -140,27 +116,67 @@ HorizontalAlignment="Stretch" Command="{Binding ListenBlueSensorCommand}" Content="听蓝色传感器" - FontSize="14" /> + FontSize="14" + Style="{StaticResource FunctionButtonStyle}" /> + + + + + + + + + + + + + + + + + + + + - - - - - - - - + BorderThickness="1"> + + + + + + + + diff --git a/Correlator/Dialog/SimplyAuditionDialog.xaml b/Correlator/Dialog/SimplyAuditionDialog.xaml index 8c1f422..5d38906 100644 --- a/Correlator/Dialog/SimplyAuditionDialog.xaml +++ b/Correlator/Dialog/SimplyAuditionDialog.xaml @@ -2,6 +2,7 @@ x:Class="Correlator.Dialog.SimplyAuditionDialog" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:b="http://schemas.microsoft.com/xaml/behaviors" xmlns:cvt="clr-namespace:Correlator.Converter" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" @@ -32,19 +33,19 @@ - + + - @@ -73,39 +74,14 @@ HorizontalAlignment="Stretch" Command="{Binding ListenRedSensorCommand}" Content="听红色传感器" - FontSize="14" /> + FontSize="14" + Style="{StaticResource FunctionButtonStyle}" /> - - - - - - - - - - - @@ -140,27 +116,67 @@ HorizontalAlignment="Stretch" Command="{Binding ListenBlueSensorCommand}" Content="听蓝色传感器" - FontSize="14" /> + FontSize="14" + Style="{StaticResource FunctionButtonStyle}" /> + + + + + + + + + + + + + + + + + + + + - - - - - - - - + BorderThickness="1"> + + + + + + + + diff --git a/Correlator/ViewModels/SimplyAuditionDialogViewModel.cs b/Correlator/ViewModels/SimplyAuditionDialogViewModel.cs index 7ccb92f..56d4e65 100644 --- a/Correlator/ViewModels/SimplyAuditionDialogViewModel.cs +++ b/Correlator/ViewModels/SimplyAuditionDialogViewModel.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices; using System.Windows; using Correlator.DataService; using Correlator.Util; @@ -65,26 +64,7 @@ public DelegateCommand ListenRedSensorCommand { get; } public DelegateCommand ListenBlueSensorCommand { get; } public DelegateCommand SensorMuteCommand { get; } - - #endregion - - #region 静音 - - //函数名不能改,否则会报找不到函数错误,dll里面定好了的 - [DllImport("user32.dll")] - private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo); - - [DllImport("user32.dll")] - private static extern byte MapVirtualKey(uint uCode, uint uMapType); - - private int GetCurrentMicVolume() - { - var enumerator = new MMDeviceEnumerator(); - - //获取音频输入设备 - var device = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); - return (int)(device.AudioEndpointVolume.MasterVolumeLevelScalar * 100); - } + public DelegateCommand ValueChangedCommand { get; } #endregion @@ -92,6 +72,7 @@ private readonly IAudioService _audioService; private readonly ICommandService _commandService; private readonly int _sampleRate; + private readonly MMDevice _device; public SimplyAuditionDialogViewModel(ISerialPortService serialPortService, IAudioService audioService, IApplicationDataService dataService, ICommandService commandService) @@ -101,11 +82,36 @@ _commandService = commandService; _sampleRate = dataService.GetSampleRateByWorkMode(RuntimeCache.WorkMode); + //获取音频输入设备 + var enumerator = new MMDeviceEnumerator(); + _device = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); CurrentVolume = GetCurrentMicVolume(); ListenRedSensorCommand = new DelegateCommand(ListenRedSensor); SensorMuteCommand = new DelegateCommand(SensorMute); ListenBlueSensorCommand = new DelegateCommand(ListenBlueSensor); + ValueChangedCommand = new DelegateCommand(SliderValueChanged); + } + + private int GetCurrentMicVolume() + { + return (int)(_device.AudioEndpointVolume.MasterVolumeLevelScalar * 100); + } + + private void SetMicVolume(int value) + { + if (value < 0) + { + _device.AudioEndpointVolume.MasterVolumeLevelScalar = 0 / 100.0f; + } + else if (value > 100) + { + _device.AudioEndpointVolume.MasterVolumeLevelScalar = 100 / 100.0f; + } + else + { + _device.AudioEndpointVolume.MasterVolumeLevelScalar = value / 100.0f; + } } private void ListenRedSensor() @@ -128,24 +134,13 @@ } } + /// + /// 静音 + /// private void SensorMute() { - if (_currentVolume > 0) - { - //静音 - keybd_event(0xAD, MapVirtualKey(0xAD, 0), 0x0001, 0); - keybd_event(0xAD, MapVirtualKey(0xAD, 0), 0x0001 | 0x0002, 0); - - CurrentVolume = 0; - } - else - { - //非静音 - keybd_event(0xAD, MapVirtualKey(0xAD, 0), 0x0001, 0); - keybd_event(0xAD, MapVirtualKey(0xAD, 0), 0x0001 | 0x0002, 0); - - CurrentVolume = GetCurrentMicVolume(); - } + CurrentVolume = 0; + SetMicVolume(_currentVolume); } private void ListenBlueSensor() @@ -168,6 +163,16 @@ } } + private void SliderValueChanged(object value) + { + if (value == null) + { + return; + } + + SetMicVolume(Convert.ToInt32(value)); + } + /// /// 停止红色传感器听音 ///