C # Implementation of Windows Media Encoder audio and video capture

Source: Internet
Author: User

My blog tells about WMS9, can implement streaming media server, but without WME (Windows Media Encoder) support, always feel the lack of something, Here's how to get the device video audio stream through the. NET implementation WME and push it to remote (of course, local) streaming media broadcast publishing point.

First, download the WME9 SDK, installation completed: This step is to experience the main WME can achieve those functions.

The reader can try it on its own installation.

Two. Create. NET WinForm application, add reference using Wmencoderlib;

Wmencoderlib is a assemble, through which we can facilitate the implementation of WME operations.

Three. Get the local screen and convert it to the video signal iwmencsource Srcaud = Srcgrp.addsource (wmenc_source_type. Wmenc_audio);
Iwmencsource Srcvid = Srcgrp.addsource (wmenc_source_type. Wmenc_video);
Srcaud.setinput ("Default_audio_device", "Device", "");
Srcvid.setinput ("ScreenCapture1", "Screencap", "");

Four. Start encoder, push to remote WMS Server/Set up the push broadcast.
Iwmencpushdistribution pushdist = (iwmencpushdistribution) encoder.broadcast;
String strservername = this. TB_ServerName.Text.Trim () + ":" + this. TB_Port.Text.Trim ();
String strpubpoint = "Tmpbroadcastpubpoint";
Remove the publishing point when the broadcast was over.
Pushdist.autoremovepublishingpoint = true;
Set the push distribution variables.
Pushdist.servername = strServerName;
Pushdist.publishingpoint = Strpubpoint;
Pushdist.template = strpubtemplate;
Initialize the encoding process.
Encoder.preparetoencode (TRUE);

Note: Here, push directly to the remote server, if the server is not in the same domain, or in the untrusted zone, the push should need to verify the user name and password. So we need to create a delegate: Encoder.onacquirecredentials + = new _iwmencoderevents_onacquirecredentialseventhandler (OnUserAuth);

public void Onuserauth (string bstrrealm, String bstrsite, ref object Pvaruser, ref object Pvarpassword, re F Object Plflags)
... {
Pvaruser = "Administrator";
Pvarpassword = "622316";
Plflags = 4;
}

Five. It's almost over here, the complete code is posted: using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using Wmencoderlib;

Namespace Project2
... {
public partial class Form1:form
... {
static bool Bdone;
Private Wmencoder encoder;
Public Form1 ()
... {
InitializeComponent ();
Encoder = new Wmencoder ();
Encoder.onstatechange + = new _iwmencoderevents_onstatechangeeventhandler (onwmestatuschange);
Encoder.onerror + = new _iwmencoderevents_onerroreventhandler (encoder_onerror);
Encoder.onacquirecredentials + = new _iwmencoderevents_onacquirecredentialseventhandler (OnUserAuth);
}

private void Btn_start_click (object sender, EventArgs e)
... {
This. Startbroadcast ();
}

private void Btn_close_click (object sender, EventArgs e)
... {
This. Stopbroadcast ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.