Take the following steps to create and run a multimedia TV tuner capture application using the LEADTOOLS Media Foundation CaptureCtrl control.
Start Visual Studio.
Choose File->New->Project... from the menu.
In the New Project dialog box, choose either "Visual C# Projects" or "VB Projects" in the Projects Type List, and choose "Windows Application " in the Templates List.
Type the project name as "Multimedia TV Tuner Capture" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK.
In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Reference..." from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and select Leadtools.MediaFoundation and click OK.
Make sure Form1 is in design view. Go to the toolbox (View->Toolbox) and drag and drop a CaptureCtrl control on the form. NOTE: If you do not have CaptureCtrl in your toolbox, select Tools->Choose Toolbox Items from the menu. Click Browse and then select Leadtools.MediaFoundation.dll from "<LEADTOOLS_INSTALLDIR>\Bin\Dotnet4\Win32" and then click Open and then click OK. After adding it to the form, set the following properties on the capture control:
Property | Value |
---|---|
Name | _capturectrl |
Anchor | Top, Bottom, Left, Right |
Go to the toolbox (View->Toolbox) and drag and drop a ProgressBar control (to be used to display capture progress) on the form (below the capture control) and set the following properties:
Property | Value |
---|---|
Name | _progress |
Anchor | Bottom, Left, Right |
Go to the toolbox (View->Toolbox) and drag and drop 4 Button controls on the bottom of the form (for Capture, Channel Up and Channel Down) and set the following properties:
Property | Value |
---|---|
Name | _buttonCapture |
Text | Capture |
Anchor | Bottom, Right |
Name | _buttonChannelUp |
Text | Up |
Anchor | Bottom, Right |
Name | _buttonChannelDown |
Text | Down |
Anchor | Bottom, Right |
Go to the toolbox (View->Toolbox) and drag and drop a Label control, a Text control, another Label control and a ProgressBar control on the bottom left of the form (for a "Channel:" label, a channel text box, a "Signal:" label and a signal strength meter) and set the following properties:
Property | Value |
---|---|
Name | label1 |
Text | Channel: |
Anchor | Bottom, Left |
Name | _textChannel |
Anchor | Bottom, Left |
Locked | True |
ReadOnly | True |
TextAlign | Center |
Name | label2 |
Text | Signal: |
Anchor | Bottom, Left |
Name | _signal |
Anchor | Bottom, Left |
Switch Form1 to code view (right-click Form1 in the solution explorer then select View Code) and add the following lines at the beginning of the file:
using System.Runtime.InteropServices;
using Leadtools.MediaFoundation;
Declare the following private variables:
private string _targetFile;
private int _captureTime;
Add an event handler to the Form1 Load event and code it as follows:
private void Form1_Load(object sender, EventArgs e)
{
if (_capturectrl.VideoDevices.Count == 0)
throw new Exception("No capture devices available");
if (_capturectrl.VideoDevices["Analog"] == null)
throw new Exception("No Analog TV capture devices available");
_capturectrl.VideoDevices["Analog"].Selected = true;
_capturectrl.UseVideoDeviceAudio = true;
_capturectrl..TargetFormats.Selection = TargetFormatType.MP4;
_targetFile = @"capture.mp4";
_captureTime = 30;
_capturectrl.PreviewSource = CapturePreview.Video;
_capturectrl.Preview = true;
DisplayChannelAndSignal();
}
Add the following helper methods as follows:
private void DisplayChannelAndSignal()
{
if (_capturectrl.TVTuner != null)
{
int currChannel = _capturectrl.TVTuner.Channel;
_textChannel.Text = currChannel.ToString();
TunerSignalStrength signal = _capturectrl.TVTuner.SignalPresent;
switch (signal)
{
case TunerSignalStrength.NoSignal:
_signal.Value = 0;
break;
case TunerSignalStrength.HasNoSignalStrength:
_signal.Value = 10;
break;
case TunerSignalStrength.SignalPresent:
_signal.Value = 100;
break;
}
}
}
private void ChangeChannel(bool up)
{
int chanMin = _capturectrl.TVTuner.ChannelMin;
int chanMax = _capturectrl.TVTuner.ChannelMax;
int currChannel = _capturectrl.TVTuner.Channel;
int currVideoChannel = _capturectrl.TVTuner.VideoSubChannel;
if (up)
{
if (currChannel == chanMax)
currChannel = chanMin;
else
currChannel++;
}
else
{
if (currChannel == chanMin)
currChannel = chanMax;
else
currChannel--;
}
_capturectrl.TVTuner.SetChannel(currChannel, -1, -1);
DisplayChannelAndSignal();
}
Add an event handler to the _capturectrl Progress event and code it as follows:
private void _capturectrl_Progress(object sender, ProgressEventArgs e)
{
_progress.Value = (int)((_progress.Maximum * e.time) / (_captureTime * 1000));
}
Add an event handler to the _capturectrl Complete event and code it as follows:
private void _capturectrl_Complete(object sender, EventArgs e)
{
MessageBox.Show("Capture Complete");
_buttonCapture.Enabled = true;
}
Add an event handler to the _buttonChannelUp Click event and code it as follows:
private void _buttonChannelUp_Click(object sender, EventArgs e)
{
if (_capturectrl.TVTuner != null)
{
ChangeChannel(true);
}
}
Add an event handler to the _buttonChannelDown Click event and code it as follows:
private void _buttonChannelDown_Click(object sender, EventArgs e)
{
if (_capturectrl.TVTuner != null)
{
ChangeChannel(false);
}
}
Add an event handler to the _buttonCapture Click event and code it as follows:
private void _buttonCapture_Click(object sender, System.EventArgs e)
{
try
{
_capturectrl.Preview = true;
_capturectrl.TargetFile = _targetFile;
_capturectrl.UseTimeLimit = true;
_capturectrl.TimeLimit = _captureTime;
_capturectrl.StartCapture(CaptureMode.Video);
_buttonCapture.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Build, and Run the program to test it.