实例介绍
【实例简介】
项目中 引用的 视频相关dll为 傲瑞相关组件,最近有用户反馈dll已过期,如需相关功能,请购买相关授权,官网:http://www.oraycn.com/
【实例截图】

【核心代码】
public partial class Form1 : Form
{
private ICapturer audioCapturer;
private ICapturer videoCapturer;
private VideoFileMaker videoFileMaker;
public Form1()
{
InitializeComponent();
this.comboBox1.SelectedIndex = 0;
this.comboBox2.SelectedIndex = 0;
Oraycn.MCapture.GlobalUtil.SetAuthorizedUser("FreeUser", "");
Oraycn.MFile.GlobalUtil.SetAuthorizedUser("FreeUser", "");
}
private void button1_Click(object sender, EventArgs e)
{
try
{
int audioSampleRate = 16000;
int channelCount = 1;
if (this.comboBox1.SelectedIndex == 0) //麦克风
{
this.audioCapturer = CapturerFactory.CreateMicrophoneCapturer(0);
((IMicrophoneCapturer)this.audioCapturer).AudioCaptured = new ESBasic.CbGeneric<byte[]>(Form1_AudioCaptured);
}
else //声卡
{
this.audioCapturer = CapturerFactory.CreateSoundcardCapturer();
((ISoundcardCapturer)this.audioCapturer).AudioCaptured = new ESBasic.CbGeneric<byte[]>(Form1_AudioCaptured);
audioSampleRate = ((ISoundcardCapturer)this.audioCapturer).SampleRate;
channelCount = ((ISoundcardCapturer)this.audioCapturer).ChannelCount;
}
int frameRate = 15 ;
System.Drawing.Size videoSize = new Size(int.Parse(this.textBox_width.Text), int.Parse(this.textBox_height.Text));
if (this.comboBox2.SelectedIndex == 0) //摄像头
{
this.videoCapturer = CapturerFactory.CreateCameraCapturer(0, videoSize, frameRate);
((ICameraCapturer)this.videoCapturer).ImageCaptured = new ESBasic.CbGeneric<Bitmap>(Form1_ImageCaptured);
}
else // 桌面
{
this.videoCapturer = CapturerFactory.CreateDesktopCapturer(frameRate, false);
((IDesktopCapturer)this.videoCapturer).ImageCaptured = new ESBasic.CbGeneric<Bitmap>(Form1_ImageCaptured);
videoSize = Screen.PrimaryScreen.Bounds.Size;
}
this.videoFileMaker = new VideoFileMaker();
this.videoFileMaker.AutoDisposeVideoFrame = true;
this.videoFileMaker.Initialize("test.mp4", VideoCodecType.H264, videoSize.Width, videoSize.Height, frameRate, AudioCodecType.AAC, audioSampleRate, channelCount, true);
this.button1.Enabled = false;
this.button2.Enabled = true;
this.comboBox1.Enabled = false;
this.comboBox2.Enabled = false;
this.panel_camera.Enabled = false;
this.label_tip.Visible = true;
//开始采集
this.audioCapturer.Start();
this.videoCapturer.Start();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
//停止采集
this.audioCapturer.Stop();
this.videoCapturer.Stop();
this.videoFileMaker.Close(true);
this.button2.Enabled = false;
this.button1.Enabled = true;
this.comboBox1.Enabled = true;
this.comboBox2.Enabled = true;
this.panel_camera.Enabled = true;
this.label_tip.Visible = false;
//清除最后一帧的显示
CbGeneric cb = new CbGeneric(this.AfterStop);
cb.BeginInvoke(null, null);
}
private void AfterStop()
{
if (this.InvokeRequired)
{
System.Threading.Thread.Sleep(1000);
this.BeginInvoke(new CbGeneric(this.AfterStop));
}
else
{
//清除最后一帧的显示
this.pictureBox1.BackgroundImage = null;
this.pictureBox1.Invalidate();
}
}
void Form1_AudioCaptured(byte[] audioData) //采集到的声音数据
{
this.videoFileMaker.AddAudioFrame(audioData);
}
//采集到的视频或桌面图像
void Form1_ImageCaptured(Bitmap img)
{
this.DisplayVideo((Bitmap)img.Clone());
this.videoFileMaker.AddVideoFrame(img);
}
private void DisplayVideo(Bitmap img)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new ESBasic.CbGeneric<Bitmap>(this.DisplayVideo), img);
}
else
{
Bitmap old = (Bitmap)this.pictureBox1.BackgroundImage;
this.pictureBox1.BackgroundImage = img;
if (old != null)
{
old.Dispose();
}
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
this.panel_camera.Visible = this.comboBox2.SelectedIndex == 0;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox1.SelectedIndex == 1)
{
MessageBox.Show("声卡采集进目前仅支持windows vista/7 及以上版本的系统!");
}
}
}
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)