WPF 录屏软件研发心得及思路分享
最近由于工程需要开始研发基于Windows的自动录屏软件,很多细节很多功能需要处理,毕竟一个完美的录屏软件不是你随随便便就可以写出来的。首先参考了大部分的录屏软件,在研发的过程中遇到了很多的问题;比如-视频加载、麦克风加载、麦克风音量调节、视频播放进度控、视频音量控制、等等很多细节部分都需要好好规划才能开始做。录屏采用的是视频帧的思维逻辑进行编写的。
目前已经基本上成型,基于WPF采用了Model - View框架进行动态加载,每个线程与线程之间采用Async异步执行,并使用线程等待;录屏基本功能包含了(展示历史录屏记录、删除、录屏、视频编码、视频播放及删除、麦克风调用(音量调节-跟随系统)、加载视频(拖拉-旋转)、系统遮罩 等);编码的核心是采用FFMPEG(这个工具真的非常强大);
这边提供几个核心代码仅供参考:
1-难点:系统遮罩核心方法(使用Windows API):
改变属性的时候触发
API方法2-难点:麦克风获取及控制
<Slider x:Name="volumeSlider" Grid.Column="7" Grid.ColumnSpan="3" Grid.Row="1" Width="100" Height="20" Minimum="0" Maximum="100" Value="100" VerticalAlignment="Center" />
1 //定义一个获取之前拉动时候的value值,然后跟当前的value对比,选择触发
2 private bool isUserChangeVolume = true;
3 private VolumeControl volumeControl;
4 //private DispatcherTimer volumeControlTimer;
5
6 /// <summary>
7 /// 加载拖动条的事件
8 /// </summary>
9 /// <param name="sender"></param>
10 /// <param name="e"></param>
11 private void volumeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
12 {
13 if (isUserChangeVolume)
14 {
15 volumeControl.MasterVolume = volumeSlider.Value;
16 }
17 }
18
19 private void InitializeAudioControl()
20 {
21 volumeControl = VolumeControl.Instance;
22 volumeControl.OnAudioNotification += volumeControl_OnAudioNotification;
23 volumeControl_OnAudioNotification(null, new AudioNotificationEventArgs() { MasterVolume = volumeControl.MasterVolume });
24
25 //volumeControlTimer = new DispatcherTimer();
26 //volumeControlTimer.Interval = TimeSpan.FromTicks(150);
27 //volumeControlTimer.Tick += volumeControlTimer_Tick;
28 }
29
30 void volumeControl_OnAudioNotification(object sender, AudioNotificationEventArgs e)
31 {
32 this.isUserChangeVolume = false;
33 try
34 {
35 this.Dispatcher.Invoke(new Action(() => { volumeSlider.Value = e.MasterVolume; }));
36 }
37 catch { }
38 this.isUserChangeVolume = true;
39 }
40
41 void volumeControlTimer_Tick(object sender, EventArgs e)
42 {
43 //获取系统主声道、左声道、右声道音量值
44 //double[] information = volumeControl.AudioMeterInformation;
45 //mMasterPBar.Value = information[0];
46 //mLeftPBar.Value = information[1];
47 //mRightPBar.Value = information[2];
48 }
3-难点:系统遮罩(其实也不能算难点,这个是API调用的时候颜色控制);
4-难点:视频旋转核心代码
旋转视频代码5-难点:录屏核心代码(这部分代码视频格式可以自行调整,颜色代码原理已经理解。)
录屏代码6-难点:屏幕画框代码(采集X,Y坐标及遮幕的宽,高)
1 /// <summary>
2 /// 设置初始区域
3 /// </summary>
4 private void SetupInitialRegion()
5 {
6 var cursorPos = System.Windows.Forms.Cursor.Position;
7 foreach (var screen in Screen.AllScreens)
8 {
9 if (screen.Bounds.Contains(cursorPos) == false)
10 {
11 continue;
12 }
13
14 var regionWidth = (double)screen.Bounds.Width / 2;
15 var regionHeight = (double)screen.Bounds.Height / 2;
16 double x = ((double)screen.Bounds.Width / 2) - (regionWidth / 2);
17 double y = ((double)screen.Bounds.Height / 2) - (regionHeight / 2);
18 x -= this.virtualScreen.X - screen.Bounds.X;
19 y -= this.virtualScreen.Y - screen.Bounds.Y;
20
21 this.startPosition = new Point(x, y);
22 this.endPosition = new Point(x + regionWidth, y + regionHeight);
23 this.UpdatePosition();
24 break;
25 }
26 }
以上是这几天研究的成果,目前还在进一步研究中,效果图如下:


