基本信息
源码名称:录制画中画(桌面+摄像头+麦克风声卡)
源码大小:7.92M
文件格式:.rar
开发语言:C#
更新时间:2020-02-21
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 10 元 
   源码介绍
使用此实例,需要购买Oraycn.的授权

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESBasic;
using Oraycn.MCapture;
using Oraycn.MFile;

namespace Oraycn.RecordPictureCompositionDemo
{
    /*
    * 本demo采用的是 语音视频采集组件MCapture 和 语音视频录制组件MFile 的免费版本。若想获取MCapture、MFile的其它版本,请联系 www.oraycn.com ,客服qq:168757008。
    * 
    */
    public partial class Form1 : Form
    {
        private ISoundcardCapturer soundcardCapturer;
        private IMicrophoneCapturer microphoneCapturer;
        private IDesktopCapturer desktopCapturer;
        private ICameraCapturer cameraCapturer;
        private IAudioMixter audioMixter;
        private VideoFileMaker videoFileMaker;
        private SilenceVideoFileMaker silenceVideoFileMaker;
        //private AudioFileMaker audioFileMaker;
        private int frameRate = 10; // 采集视频的帧频
        private bool sizeRevised = false;// 是否需要将图像帧的长宽裁剪为4的整数倍
        private  bool isRecording = false;
        private bool isParsing = false;
        private Timer timer;
        private int seconds = 0;
        private bool justRecordVideo = false;
        

        public Form1()
        {
            InitializeComponent();
            Oraycn.MCapture.GlobalUtil.SetAuthorizedUser("FreeUser", "");
            Oraycn.MFile.GlobalUtil.SetAuthorizedUser("FreeUser", "");

            this.timer = new Timer();
            this.timer.Interval = 1000;
            this.timer.Tick  = new EventHandler(timer_Tick);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (this.isRecording && !this.isParsing)
            {
                var ts = new TimeSpan(0, 0,   seconds);
                this.label_time.Text = ts.Hours.ToString("00")   ":"   ts.Minutes.ToString("00")   ":"   ts.Seconds.ToString("00");
                if (this.videoFileMaker != null)
                {
                    this.label_info.Text = this.videoFileMaker.ToBeProcessedVideoFrameCount.ToString() ;
                }
            }
        }

        #region 开始录制
        private void button_start_Click(object sender, EventArgs e)
        {
            //TODO 开始录制桌面,依据 声音复选框 来选择使用 声卡 麦克风 还是混合录制, 图像复选框来选择 图像的采集器
            try
            {
                int audioSampleRate = 16000;
                int channelCount = 1;
                seconds = 0;

                System.Drawing.Size videoSize_desktop ;
                
                

                #region 设置采集器
                
                    //桌面采集器
                    
                    //如果需要录制鼠标的操作,第二个参数请设置为true
                    this.desktopCapturer = CapturerFactory.CreateDesktopCapturer(frameRate, false);
                    this.desktopCapturer.ImageCaptured  = new CbGeneric<Bitmap>(this.DesktopImageCaptured);
                    videoSize_desktop = this.desktopCapturer.VideoSize; 
                
                    //摄像头采集器
                    
                    this.cameraCapturer = CapturerFactory.CreateCameraCapturer(0, new System.Drawing.Size(640, 480), frameRate);
                    this.cameraCapturer.ImageCaptured  = new CbGeneric<Bitmap>(this.VideoImageCaptured);
               

                if (this.checkBox_micro.Checked)
                {
                    //麦克风采集器
                    this.microphoneCapturer = CapturerFactory.CreateMicrophoneCapturer(0);
                    this.microphoneCapturer.CaptureError  = new CbGeneric<Exception>(this.CaptureError);
                }

                if (this.checkBox_soundCard.Checked)
                {
                    //声卡采集器 【目前声卡采集仅支持vista以及以上系统】
                    this.soundcardCapturer = CapturerFactory.CreateSoundcardCapturer();
                    this.soundcardCapturer.CaptureError  = this.CaptureError;
                    audioSampleRate = this.soundcardCapturer.SampleRate;
                    channelCount = this.soundcardCapturer.ChannelCount;
                }

                if (this.checkBox_micro.Checked && this.checkBox_soundCard.Checked)
                {
                    //混音器
                    this.audioMixter = CapturerFactory.CreateAudioMixter(this.microphoneCapturer, this.soundcardCapturer,
                        SoundcardMode4Mix.DoubleChannel, true);
                    this.audioMixter.AudioMixed  = audioMixter_AudioMixed;
                    audioSampleRate = this.audioMixter.SampleRate;
                    channelCount = this.audioMixter.ChannelCount;
                }
                else if (this.checkBox_micro.Checked)
                {
                    this.microphoneCapturer.AudioCaptured  = audioMixter_AudioMixed;
                }
                else if (this.checkBox_soundCard.Checked)
                {
                    this.soundcardCapturer.AudioCaptured  = audioMixter_AudioMixed;
                }
                #endregion


                #region //开始采集
                if (this.checkBox_micro.Checked)
                {
                    this.microphoneCapturer.Start();
                }
                if (this.checkBox_soundCard.Checked)
                {
                    this.soundcardCapturer.Start();
                }

                
                    this.cameraCapturer.Start();
                
               
                    this.desktopCapturer.Start();
                
                #endregion


                #region //录制组件


                    this.sizeRevised = (videoSize_desktop.Width % 4 != 0) || (videoSize_desktop.Height % 4 != 0);
                    if (this.sizeRevised)
                    {
                        videoSize_desktop = new System.Drawing.Size(videoSize_desktop.Width / 4 * 4, videoSize_desktop.Height / 4 * 4);
                    }

                    if (!this.checkBox_micro.Checked && !this.checkBox_soundCard.Checked)
                    {
                        //只录制 图像
                        this.justRecordVideo = true;
                        this.silenceVideoFileMaker = new SilenceVideoFileMaker();
                        this.silenceVideoFileMaker.Initialize("test.mp4", VideoCodecType.H264, videoSize_desktop.Width, videoSize_desktop.Height, frameRate, VideoQuality.Middle);
                    }
                    else
                    {
                        // 录制声音和图像
                        this.justRecordVideo = false;
                        this.videoFileMaker = new VideoFileMaker();
                        this.videoFileMaker.Initialize("test.mp4", VideoCodecType.H264, videoSize_desktop.Width, videoSize_desktop.Height, frameRate, VideoQuality.High,
                            AudioCodecType.AAC, audioSampleRate, channelCount, true);

                    }
                
                #endregion



                this.isRecording = true;
                this.isParsing = false;

                this.timer.Start();

                this.checkBox_micro.Enabled = false;
                this.checkBox_soundCard.Enabled = false;
                

                this.button_start.Enabled = false;


            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }
        #region CaptureError
        void CaptureError(Exception obj)
        {

        } 
        #endregion

        #region VideoImageCaptured
        private Bitmap videoImgRecorded;
        //采集到的视频图像
        void VideoImageCaptured(Bitmap img)
        {
            if (this.isRecording && !this.isParsing)
            {
               
                this.videoImgRecorded = img;
                
            }
        } 
        #endregion

        
        #region DesktopImageCaptured
        
        //采集到的桌面图像
        void DesktopImageCaptured(Bitmap img)
        {
            if (this.isRecording && !this.isParsing)
            {
                if (this.videoImgRecorded != null)
                {
                    //这里要裁剪
                    Bitmap desktopImgRecorded = img;
                    if (this.sizeRevised) // 对图像进行裁剪,  MFile要求录制的视频帧的长和宽必须是4的整数倍。
                    {
                        desktopImgRecorded = ESBasic.Helpers.ImageHelper.RoundSizeByNumber(img, 4);
                        img.Dispose();
                    }
                   
                    //合并图像
                    Graphics g = Graphics.FromImage(desktopImgRecorded);
                    g.DrawImage(this.videoImgRecorded, new Rectangle(new Point((desktopImgRecorded.Width - this.videoImgRecorded.Width), (desktopImgRecorded.Height - this.videoImgRecorded.Height)),
                        desktopImgRecorded.Size));
                    g.Dispose();

                    if (!this.justRecordVideo)
                    {
                        this.videoFileMaker.AddVideoFrame(desktopImgRecorded);
                    }
                    else
                    {
                        this.silenceVideoFileMaker.AddVideoFrame(desktopImgRecorded);
                    }
                }
            }
        }
        #endregion

        #region audioMixter_AudioMixed
        void audioMixter_AudioMixed(byte[] audioData)
        {
            if (this.isRecording && !this.isParsing)
            {

                if (!this.justRecordVideo)
                {
                    this.videoFileMaker.AddAudioFrame(audioData);
                }


            }
        } 
        #endregion
        #endregion

        #region 暂停
        private void button_parse_Click(object sender, EventArgs e)
        {
            //TODO 暂停当前录制或恢复录制
            //TODO label 中显示实际录制的时间,需要考虑暂停和恢复这种情况。 格式为 hh:mm:ss
            if (this.isParsing)
            {
                this.isParsing = false;
            }
            else
            {
                this.isParsing = true;
            }
            this.button_parse.Text = (!this.isParsing ? "暂停" : "恢复");

        }
        
        #endregion


        #region 结束录制
        private void button_stop_Click(object sender, EventArgs e)
        {
            this.checkBox_micro.Enabled = true;
            this.checkBox_soundCard.Enabled = true;


            this.button_start.Enabled = true;
            this.button_parse.Text = "暂停";

            if (this.checkBox_micro.Checked) // 麦克风
            {
                this.microphoneCapturer.Stop();
            }
            if (this.checkBox_soundCard.Checked) //声卡
            {
                this.soundcardCapturer.Stop();
            }

            this.cameraCapturer.Stop();


            this.desktopCapturer.Stop();


            if (!this.justRecordVideo)
            {
                this.videoFileMaker.Close(true);
            }
            else
            {
                this.silenceVideoFileMaker.Close(true);
            }


            this.isRecording = false;
            MessageBox.Show("录制完成!");
        }
        #endregion

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.isRecording)
            {
                MessageBox.Show("正在录制中,退出前请先停止录制!");
                e.Cancel = true;
                return;
            }

            e.Cancel = false;
        }

       






        
    }
}