This example demonstrates how to select devices and receive data from Audio & Video Capture Devices.
Requirements (Components): 4 Comboboxes, 2 Buttons, 1
Panel, 1 Picturebox
Requirements (Assemblies): StreamTools.NET & MediaBase
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using StreamCoders.Devices;
using StreamCoders.Wave;
namespace DeviceCapture
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
cam = new CamCapture();
var deviceList = cam.GetDeviceList();
foreach (string deviceName in deviceList)
{
comboBox1.Items.Add(deviceName);
}
comboBox1.SelectedIndex = 0;
waveout = new WaveOutput();
List<AudioDeviceInfo> devList = waveout.Devices;
foreach (AudioDeviceInfo ad in devList)
{
comboBox2.Items.Add(ad.Name);
}
comboBox2.SelectedIndex = 0;
wavein = new WaveInput();
List<AudioDeviceInfo> inDevList = wavein.Devices;
foreach (AudioDeviceInfo ad in inDevList)
{
comboBox3.Items.Add(ad.Name);
}
comboBox3.SelectedIndex = 0;
}
private void button2_Click(object sender, EventArgs e)
{
if(comboBox4.SelectedIndex == -1)
{
MessageBox.Show("Please select Capture Device Metrics");
return;
}
if(timer1.Enabled == true)
{
MessageBox.Show("Already Started.");
return;
}
AudioDeviceInfo outpDev = waveout.FindDeviceByName(comboBox2.SelectedItem.ToString());
if (outpDev != null)
{
waveout.SampleRate = 44100;
waveout.Channels = 1;
waveout.BitsPerSample = 16;
waveout.Init();
waveout.OpenDevice(outpDev.Name);
}
else
{
MessageBox.Show("Audio Device not found");
return;
}
AudioDeviceInfo inpDev = wavein.FindDeviceByName(comboBox3.SelectedItem.ToString());
if (inpDev != null)
{
wavein.SampleRate = 44100;
wavein.Channels = 1;
wavein.BitsPerSample = 16;
wavein.OpenDevice(inpDev.Name);
wavein.Start();
}
else
{
MessageBox.Show("Audio Device not found");
return;
}
cam.SelectMetrics(selectedDeviceMetrics[comboBox4.SelectedIndex]);
bool res = cam.Start();
if (res == false)
{
MessageBox.Show("Unable to start Camera");
}
metrics = cam.Metrics;
pictureBox1.Width = metrics.Width;
pictureBox1.Height = metrics.Height;
wavein.ClearBuffers();
timer1.Interval = 50;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (wavein.SamplesAvailable)
waveout.Enqueue(wavein.GetAllData());
byte[] buffer = cam.GetFrame();
if (buffer == null)
return;
Bitmap pBmp = StreamCoders.Tools.Visuals.ByteArrayToBitmap_RGB24((uint)metrics.Width, (uint)metrics.Height, buffer);
pBmp.RotateFlip(RotateFlipType.Rotate180FlipX);
using (Graphics pGfx = pictureBox1.CreateGraphics())
{
pGfx.DrawImage(pBmp, 0, 0);
}
}
private void button1_Click_1(object sender, EventArgs e)
{
timer1.Stop();
System.Threading.Thread.Sleep(50 + 20);
cam.Stop();
wavein.CloseDevice();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox4.Items.Clear();
selectedDeviceMetrics = cam.SelectDevice(comboBox1.Text);
foreach (CamMetrics cm in selectedDeviceMetrics)
{
comboBox4.Items.Add(String.Format("{0}x{1}@{2}", cm.Width, cm.Height, (int)cm.Fps));
}
comboBox4.SelectedIndex = 0;
}
private CamCapture cam;
private CamMetrics metrics;
private WaveInput wavein;
private WaveOutput waveout;
private List<CamMetrics> selectedDeviceMetrics;
}
}