⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ~av.cs

📁 语音视频功能 里面实现了基本的QQ与语音对话
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;using System.IO;
using gowk.multimedia;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using System.Net;
namespace gowk.multimedia
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	
	public class AV:System.IDisposable
	{

		public AV(Control local,Control remote)
		{
			this.local=local;
			this.remote=remote;
		}
		#region IDisposable 成员

		public void Dispose()
		{
			if(this.vc!=null)
			{
				this.vc.Preview=false;
				this.vc.Stop();
				this.vc.Disconnect();
				this.vc.Close();
			}
			if(this.chanel!=null)
			{
				try
				{
					this.chanel.Disconnect();
					this.chanel.Close();
				}
				catch(System.Exception ex)
				{
					MessageBox.Show(ex.Message);
				}
			}
			if(this.wi!=null)
			{
			//	this.wi.Reset();
				this.wi.Stop();
			}
			if(this.dd!=null)
			{
				this.dd.Close();
			}
			if(this.wo!=null)
			{
				this.wo.Stop();
			//	this.wo.Reset();
			//	this.wo.Dispose();
			}
			if(this.mixer!=null)
			{
				this.mixer.Close();
			}
		}

		#endregion
		Mixer mixer;
		Mixer.MixerControlDetail dt,dt2;
		DrawDib dd;
		AVChanel chanel;
		gowk.multimedia.VideoCapturer vc;
		
		WaveIn wi;
		WaveOut wo;WAVEFORMATEX wf;
		Control local,remote;
		#region initial
	/*	public void InitializeAV()
		{
			this.IniAudio();
			this.IniVideo();
		}*/
		public void Initial()
		{
			this.IniChanel();
			this.IniAudio();
			this.IniVideo();
		}
		#region audio
		public void IniAudio()
		{
			wf=new WAVEFORMATEX();
			wf.cbSize=0;
			wf.nChannels=1;
			wf.nSamplesPerSec=8000;
			wf.wBitsPerSample=16;
			wf.nBlockAlign=2;
			wf.nAvgBytesPerSec=16000;
			wf.wFormatTag=1;//pcm


			wo=new WaveOut(0,wf,4096,20);
			wo.WaveOutError+=new WaveErrorEventHandler(wo_WaveInError);
			wi=new WaveIn(0,wf,4096,20);
			wi.WaveInError+=new WaveErrorEventHandler(wo_WaveInError);
			wi.WaveCaptured+=new WaveBufferEventHandler(wi_WaveCaptured);
			this.wo.Start();
			this.wi.Start();

			
			this.mixer=new Mixer(this.local);
			dt=new Mixer.MixerControlDetail(this.mixer,Mixer.MIXERLINE_COMPONENTTYPE_DST_SPEAKERS);
			dt2=new Mixer.MixerControlDetail(this.mixer,0x00001000+3);
			
		}
		#endregion
		#region video
		public void IniVideoCapture()
		{
			this.vc=new VideoCapturer(this.local,0);
			this.vc.ConnectDevice();
			this.vc.SetPreviewRate(66);
			this.vc.Preview=true;
			gowk.multimedia.CaptureParms cp=this.vc.CaptureParms;
			cp.fAbortLeftMouse=cp.fAbortRightMouse=false;
			cp.fYield=true;
			this.vc.CaptureParms=cp;
			gowk.multimedia.BITMAPINFO h=this.vc.BITMAPINFO;
			h.bmiHeader.biBitCount=24;
			h.bmiHeader.biWidth=160;
			h.bmiHeader.biHeight=120;
			this.vc.BITMAPINFO=h;
			this.vc.VideoCaptured+=new VideoCaptureEventHandler(vc_VideoCaptured);
			this.vc.CaptureWithOutFile();
		}
		private void IniVideoRender()
		{
			BITMAPINFOHEADER bmi=new BITMAPINFOHEADER();

			bmi.biCompression =(int) BI.BI_RGB;
			bmi.biWidth = 160;
			bmi.biHeight = 120;
			bmi.biPlanes = 1;
			bmi.biBitCount = 24;
			bmi.biXPelsPerMeter = 0;
			bmi.biYPelsPerMeter = 0;
			bmi.biClrUsed = 0;
			bmi.biClrImportant = 0;
			bmi.biSizeImage=57600;
			bmi.biSize=Marshal.SizeOf(bmi);
			dd=new DrawDib();
			dd.Control=this.remote;
			dd.BITMAPINFOHEADER=bmi;
			dd.Open();
		}
		private void IniVideo()
		{
		//	this.IniVideoCompress();
			this.IniVideoRender();
			this.IniVideoCapture();
		}
		#endregion
		#region
		public void IniChanel()
		{
			chanel=new AVChanel();
			this.chanel.AudioData+=new AVChanel.AVChanelEventHandler(chanel_AudioData);
			this.chanel.VideoData+=new AVChanel.AVChanelEventHandler(chanel_VideoData);
			this.chanel.Open();
		}

		#endregion
		#endregion
		public void Start(string ip,int p)
		{
			this.chanel.Connect(ip,p);
		}
		public AVChanel Chanel
		{
			get{return this.chanel;}
		}

		//SetWindowPos(hCapWnd, 0, 25, 25, bmi.bmiHeader.biWidth,bmi.bmiHeader.biHeight,0);
		private void chanel_AudioData(object sender, AVChanel.AVChanelEventArgs e)
		{
			this.wo.Write(e.Data);
		}

		private void chanel_VideoData(object sender, AVChanel.AVChanelEventArgs e)
		{
			//	byte[] decompressdata=this.dp.Process(e.Data);
			this.dd.Draw(e.Data);
		}
		private void vc_VideoCaptured(object sender, VIDEOHDR hdr)
		{
			byte[] ddd=new byte[hdr.dwBytesUsed];
			Marshal.Copy(hdr.lpData,ddd,0,hdr.dwBytesUsed);
			this.dd.Draw(ddd);
			if(this.chanel==null || !this.chanel.Connected)return;
			try
			{
				byte[] data=new byte[hdr.dwBytesUsed];
				Marshal.Copy(hdr.lpData,data,0,hdr.dwBytesUsed);
		//		byte[] bs=this.icm.Process(data);
			//	ide.Process(bs);
			//	byte[] compresseddata=this.cp.Process(data);
			//	byte[] decompressdata=this.dp.Process(compresseddata);
			//	this.dd.Draw(decompressdata);
			//	this.dd.Draw(data);
			//	if(this.chanel!=null &&this.chanel.Connected)
			//	{
					this.chanel.SendVideo(data);
			//	}
			}
			catch(System.Exception ex)
			{
			//	MessageBox.Show(ex.Message);
			//	throw;
				gowk.utility.Diagnostics.Debug.Write(ex);
				System.Diagnostics.Trace.WriteLine(ex.Message+":"+ex.StackTrace);
			}
		}
		private void wi_WaveCaptured(object sender, WAVEHDR hdr)
		{
			if(this.chanel==null ||!this.chanel.Connected)return;
			byte[] data=new byte[hdr.dwBytesRecorded];
			System.Runtime.InteropServices.Marshal.Copy(hdr.lpData,data,0,data.Length);
			this.chanel.SendAudio(data);
		
		//	this.chanel.Test(data);
		//	for(int i=0;i<data.Length;i+=300)
		//	{
		//		this.progressBar1.Value=System.BitConverter.ToUInt16(data,i);
		//	}
			//	this.wo.Write(data);
		//	this.wo.Write(data);
		//	if(this.PlayBackVolumeControl==null)return;
		/*	lock(this.PlayBackVolumeControl)
			{
				for(int i=0;i<data.Length;i+=160)
				{
					int v=System.BitConverter.ToUInt16(data,i);
					v=v-32768;
					this.PlayBackVolumeControl.Value=32767-(int)System.Math.Abs(v);
				}
			}*/
	/*		if(this.cansent)
			{
				byte[] dst=new byte[(int)(data.Length/16)];
				System.Diagnostics.Trace.WriteLine(dst.Length);
			//	this.g729.Encode(data,dst);
				this.sock.Send(dst);
				System.Diagnostics.Trace.WriteLine("sent a audio  packet");
			}*/
		//	this.wo.Write(this.chanel.de(this.chanel.en(data)));
		}

		private void wo_WaveInError(object sender, MultimediaException e)
		{
			System.Diagnostics.Trace.WriteLine(e.Message);
		}
		public int LocalPort
		{
			get{return this.chanel.LocalPort;}
		}
		public ProgressBar PlayBackVolumeControl;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -