欢迎光临
我们一直在努力

在服务器端录制语音视频

public partial class RecordForm : Form { private MultimediaServer multimediaServer; private OMCS.Passive.Audio.MicrophoneConnector microphoneConnector; private OMCS.Passive.Video.dynamiccameraconnector dynamicCameraConnector; private IMultimediaManager multimediaManager; private BaseMaker maker; private System.Threading.Timer videoTimer; private recordmode recordMode = RecordMode.AudioAndVideo; public RecordForm(MultimediaServer server) { InitializeComponent(); this.comboBox_mode.SelectedIndex = 0; this.multimediaServer = server; this.label_port.Text = this.multimediaServer.Port.ToString(); //将服务端虚拟为一个OMCS客户端,并连接上OMCS服务器。 this.multimediaManager = MultimediaManagerFactory.GetSingleton(); this.multimediaManager.Initialize("_server", "", "127.0.0.1", this.multimediaServer.Port);//服务端以虚拟用户登录   } //在线用户列表 private void comboBox1_DropDown(object sender, EventArgs e) { List<string> list = this.multimediaServer.GetOnlineUserList(); list.Remove("_server"); //将虚拟用户排除在外 this.comboBox1.DataSource = list; } //开始录制视频 private void button1_Click(object sender, EventArgs e) { if (this.comboBox1.SelectedItem == null) { MessageBox.Show("没有选中目标用户!"); return; } string destUserID = this.comboBox1.SelectedItem.ToString(); this.recordMode = (RecordMode)this.comboBox_mode.SelectedIndex; //摄像头连接器 if (this.recordMode != RecordMode.JustAudio) { this.dynamicCameraConnector = new Passive.Video.DynamicCameraConnector(); this.dynamicCameraConnector.MaxIdleSpan4BlackScreen = 0; this.dynamicCameraConnector.ConnectEnded += new ESBasic.CbGeneric<ConnectResult>(cameraConnector1_ConnectEnded); this.dynamicCameraConnector.BeginConnect(destUserID); } //麦克风连接器 if (this.recordMode != RecordMode.JustVideo) { this.microphoneConnector = new Passive.Audio.MicrophoneConnector(); this.microphoneConnector.Mute = true; //在服务器上不播放出正在录制的声音  this.microphoneConnector.ConnectEnded += new ESBasic.CbGeneric<ConnectResult>(microphoneConnector1_ConnectEnded); this.microphoneConnector.AudioDataReceived += new CbGeneric<List<byte[]>>(microphoneConnector_AudioDataReceived); this.microphoneConnector.BeginConnect(destUserID); } this.label1.Text = string.Format("正在连接{0}的设备......" ,destUserID); this.Cursor = Cursors.WaitCursor; this.button1.Enabled = false; this.comboBox1.Enabled = false; this.comboBox_mode.Enabled = false; } //录制接收到的语音数据 void microphoneConnector_AudioDataReceived(List<byte[]> dataList) { if (this.maker != null) { foreach (byte[] audio in dataList) { if (this.recordMode == RecordMode.AudioAndVideo) { ((VideoFileMaker)this.maker).AddAudioFrame(audio); } else if (this.recordMode == RecordMode.JustAudio) { ((AudioFileMaker)this.maker).AddAudioFrame(audio); } else { } } } } void microphoneConnector1_ConnectEnded(ConnectResult obj) { this.ConnectComplete(); } void cameraConnector1_ConnectEnded(ConnectResult obj) { this.ConnectComplete(); } private int connectCompleteCount = 0; private void ConnectComplete() { ++this.connectCompleteCount; if (this.recordMode == RecordMode.AudioAndVideo) { if (this.connectCompleteCount == 2)//当语音、视频 都连接完成后,才正式启动录制。  { System.Threading.Thread.Sleep(500); this.Ready(); } } else { System.Threading.Thread.Sleep(500); this.Ready(); } } //初始化用于录制的FileMaker private void Ready() { if (this.InvokeRequired) { this.BeginInvoke(new CbGeneric(this.Ready)); } else { try { this.Cursor = Cursors.Default; if (this.recordMode == RecordMode.AudioAndVideo) { this.maker = new VideoFileMaker(); ((VideoFileMaker)this.maker).Initialize(this.dynamicCameraConnector.OwnerID + ".mp4", VideoCodecType.H264, this.dynamicCameraConnector.VideoSize.Width, this.dynamicCameraConnector.VideoSize.Height, 10, AudioCodecType.AAC, 16000, 1, true); this.videoTimer = new System.Threading.Timer(new System.Threading.TimerCallback(this.Callback), null, 0, 100); } else if (this.recordMode == RecordMode.JustAudio) { this.maker = new AudioFileMaker(); ((AudioFileMaker)this.maker).Initialize(this.microphoneConnector.OwnerID + ".mp3", AudioCodecType.MP3, 16000, 1); } else { this.maker = new SilenceVideoFileMaker(); ((SilenceVideoFileMaker)this.maker).Initialize(this.dynamicCameraConnector.OwnerID + ".mp4", VideoCodecType.H264, this.dynamicCameraConnector.VideoSize.Width, this.dynamicCameraConnector.VideoSize.Height, 10); this.videoTimer = new System.Threading.Timer(new System.Threading.TimerCallback(this.Callback), null, 0, 100); } this.label1.Text = "正在录制......"; this.label1.Visible = true; this.button1.Enabled = false; this.button2.Enabled = true; } catch (Exception ee) { MessageBox.Show(ee.Message); } } } private int callBackCount = -1; //定时获取视频帧,并录制 private void Callback(object state) { if (this.maker != null) { Bitmap bm = this.dynamicCameraConnector.GetCurrentImage(); if (bm != null) { ++this.callBackCount; if (this.recordMode == RecordMode.AudioAndVideo) { ((VideoFileMaker)this.maker).AddVideoFrame(bm); } else if (this.recordMode == RecordMode.JustVideo) { ((SilenceVideoFileMaker)this.maker).AddVideoFrame(bm); } else { } } else { } } } //停止录制 private void button2_Click(object sender, EventArgs e) { try { this.callBackCount = -1; if (this.videoTimer != null) { this.videoTimer.Dispose(); this.videoTimer = null; } this.connectCompleteCount = 0; if (this.recordMode != RecordMode.JustAudio) { this.dynamicCameraConnector.Disconnect(); this.dynamicCameraConnector = null; } if (this.recordMode != RecordMode.JustVideo) { this.microphoneConnector.Disconnect(); this.microphoneConnector = null; } this.button1.Enabled = true; this.button2.Enabled = false; this.label1.Visible = false; this.comboBox1.Enabled = true; this.comboBox_mode.Enabled = true; this.maker.Close(true); this.maker = null; MessageBox.Show("生成视频文件成功!"); } catch (Exception ee) { MessageBox.Show("生成视频文件失败!"+ ee.Message); } } }

  • 海报
海报图正在生成中...
赞(0) 打赏
声明:
1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。
2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。
3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。
4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。
文章名称:《在服务器端录制语音视频》
文章链接:https://www.456zj.com/5839.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址