Capturing audio Events
Code:Dim WithEvents Encoder As WMEncoder Private Sub Encoder_OnStateChange(ByVal enumState As WMEncoderLib.WMENC_ENCODER_STATE) ' Wait until the encoding process stops before ' exiting the application. If enumState = WMENC_ENCODER_RUNNING Then ' TODO: Handle running state. ElseIf enumState = WMENC_ENCODER_PAUSED Then ' TODO: Handle paused state. ElseIf enumState = WMENC_ENCODER_STOPPED Then ' End the application. End Else ' TODO: Handle other encoder states. End If End Sub Private Sub Form_Load() ' Create a WMEncoder object. Set Encoder = New WMEncoder ' Retrieve the source group collection and add a source group. Dim SrcGrpColl As IWMEncSourceGroupCollection Set SrcGrpColl = Encoder.SourceGroupCollection Dim SrcGrp As IWMEncSourceGroup2 Set SrcGrp = SrcGrpColl.Add("SG_1") ' Add a video and audio source to the source group. Dim SrcVid As IWMEncVideoSource2 Dim SrcAud As IWMEncAudioSource Set SrcVid = SrcGrp.AddSource(WMENC_VIDEO) Set SrcAud = SrcGrp.AddSource(WMENC_AUDIO) ' Identify the source files to encode. SrcVid.SetInput "C:\\InputFile.mpg" SrcAud.SetInput "C:\\InputFile.mpg" ' Choose a profile from the collection. Dim ProColl As IWMEncProfileCollection Dim Pro As IWMEncProfile Dim i As Integer Dim lLength As Long Set ProColl = Encoder.ProfileCollection lLength = ProColl.Count For i = 0 To lLength - 1 Set Pro = ProColl.Item(i) If Pro.Name = "Windows Media Video 8 for Local Area Network (384 Kbps)" Then SrcGrp.Profile = Pro Exit For End If Next ' Fill in the description object members. Dim Descr As IWMEncDisplayInfo Set Descr = Encoder.DisplayInfo Descr.Author = "Author name" Descr.Copyright = "Copyright information" Descr.Description = "Text description of encoded content" Descr.Rating = "Rating information" Descr.Title = "Title of encoded content" ' Specify a file object in which to save encoded content. Dim File As IWMEncFile Set File = Encoder.File File.LocalFileName = "C:\\OutputFile.wmv" ' Start the encoding process. Encoder.Start End Sub
Broadcasting a Live Stream Using the Predefined UI
Code:The following example shows how to create the predefined user interface and broadcast live multimedia content from the local computer. The audio and video sources are configured to use the default sound card and capture card. Use a blank form for this example. ' Create WMEncoderApp and WMEncoder objects. Dim Encoder As WMEncoder Dim EncoderApp As WMEncoderApp Private Sub Form_Load() Set EncoderApp = New WMEncoderApp Set Encoder = EncoderApp.Encoder ' Display the predefined Encoder UI. EncoderApp.Visible = True ' Specify the source for the input stream. Dim SrcGrpColl As IWMEncSourceGroupCollection Dim SrcGrp As IWMEncSourceGroup Dim SrcVid As IWMEncSource Dim SrcAud As IWMEncSource Set SrcGrpColl = Encoder.SourceGroupCollection Set SrcGrp = SrcGrpColl.Add("SG_1") Set SrcVid = SrcGrp.AddSource(WMENC_VIDEO) Set SrcAud = SrcGrp.AddSource(WMENC_AUDIO) SrcVid.SetInput "DEVICE://Default_Video_Device" SrcAud.SetInput "DEVICE://Default_Audio_Device" ' Specify a profile. Dim ProColl As IWMEncProfileCollection Dim Pro As IWMEncProfile Dim i As Integer Set ProColl = Encoder.ProfileCollection For i = 0 To ProColl.Count - 1 Set Pro = ProColl.Item(i) If Pro.Name = "Windows Media Video 8 for Local Area Network (384 Kbps)" Then SrcGrp.Profile = Pro Exit For End If Next ' Create a broadcast. Dim BrdCst As IWMEncBroadcast Set BrdCst = Encoder.Broadcast BrdCst.PortNumber(WMENC_PROTOCOL_HTTP) = 8080 ' Start the encoding process. Encoder.Start End Sub
Configuring Multiple Source Groups
Code:The following example shows how you can set up two source groups with audio and video content. The first source group uses a file (C:\InputFile.mpg), and the second source group uses the default sound card and capture card. The result is broadcasted from the local computer (http://computer_name:8080). For information about enumerating the audio and video devices on your system, see the Listing All Devices (Visual Basic) example. Sub Main() ' Create a Windows Media Encoder object. Dim Encoder As WMEncoder Set Encoder = New WMEncoder ' Create a source group collection object from the WMEncoder object. Dim SrcGrpColl As IWMEncSourceGroupCollection Set SrcGrpColl = Encoder.SourceGroupCollection ' Create a profile collection object from the WMEncoder object. Dim ProColl As IWMEncProfileCollection Set ProColl = Encoder.ProfileCollection ' Add a source group named SG1 to the collection. ' Create a source object for each type of multimedia content ' in the source group. Dim SrcGrp1 As IWMEncSourceGroup2 Dim SrcAud1 As IWMEncAudioSource Dim SrcVid1 As IWMEncVideoSource2 Set SrcGrp1 = SrcGrpColl.Add("SG1") Set SrcAud1 = SrcGrp1.AddSource(WMENC_AUDIO) Set SrcVid1 = SrcGrp1.AddSource(WMENC_VIDEO) ' Create a second source group named SG2, and two source objects. Dim SrcGrp2 As IWMEncSourceGroup2 Dim SrcAud2 As IWMEncAudioSource Dim SrcVid2 As IWMEncVideoSource2 Set SrcGrp2 = SrcGrpColl.Add("SG2") Set SrcAud2 = SrcGrp2.AddSource(WMENC_AUDIO) Set SrcVid2 = SrcGrp2.AddSource(WMENC_VIDEO) ' Create an IWMEncBroadcast object and specify a port and a protocol. Dim Brdcst As IWMEncBroadcast Set Brdcst = Encoder.Broadcast Brdcst.PortNumber(WMENC_PROTOCOL_HTTP) = 8080 ' Specify the input for the sources in the first source group. ' For this example, source group 1 uses file sources. SrcAud1.SetInput "C:\InputFile.mpg" SrcVid1.SetInput "C:\InputFile.mpg" ' Create a profile object. For brevity, this example uses the first ' profile in the collection. Then specify this profile object as ' the profile to use in source group 1. Dim Pro As IWMEncProfile Set Pro = ProColl.Item(0) SrcGrp1.Profile = Pro ' Specify the input sources for source group 2. In this example, ' the sources are the default audio and video devices. ' Set the profile for source group 2 to the same profile object. SrcAud2.SetInput "DEVICE://Default_Audio_Device" SrcVid2.SetInput "DEVICE://Default_Video_Device" SrcGrp2.Profile = Pro ' Set source group 1 to roll over automatically to source group 2. ' -1 indicates that the rollover happens when source group 1 ' has been encoded. SrcGrp1.SetAutoRollover -1, "SG2" ' Start encoding. Encoder.Start ' For this example, use a message box to stop the application when you ' have finished encoding. MsgBox "Click OK to stop encoding." End Sub
Controlling a Digital Device (Visual Basic)
Code:Controlling a Digital Device (Visual Basic) This example shows how to: Use a digital device as a source. Use VCR-style buttons to forward, rewind, play, and stop the tape. View the device output as you cue the tape before encoding. Use events to monitor changes in state. This example uses a pre-preview to display the stream before encoding begins, and a preview of the stream during encoding. To use this example, you need: A form (Form1). A frame (PreviewFrame). Four VCR-style buttons (btnREW, btnPLAY, btnFF, and btnSTOP). A button to start the encoding process (btnEncode). A label (Label1) for displaying the state of the device. In addition to the Windows Media Encoder reference, you must also add the Windows Media Encoder Device Control and the Windows Media Encoder Preview Control references to your project. It is also assumed that you have a digital device connected to the computer. The Windows Media Encoder SDK supports digital video (DV) devices connected to an IEEE 1394 digital video port, and video tape recorder (VTR) devices connected through a COM port using the Sony RS422 protocol. Option Explicit 'Declare variables. Dim WithEvents Encoder As WMEncoder Dim SrcGrpColl As IWMEncSourceGroupCollection Dim SrcGrp As IWMEncSourceGroup2 Dim SrcAud As IWMEncSource Dim SrcVid As IWMEncVideoSource Dim ProColl As IWMEncProfileCollection Dim Pro As IWMEncProfile Dim File As IWMEncFile Dim DCPlugMgr As IWMEncDeviceControlPluginInfoManager Dim PlugInfo As IWMEncPluginInfo Dim DCColl As IWMEncDeviceControlCollection Dim DControl As IWMEncDeviceControl Dim DCPlugin As IWMEncDeviceControlPlugin Dim DVColl_Preview As IWMEncDataViewCollection Dim Preview As WMEncDataView Dim PrePreview As WMEncPrepreview Dim lPreviewStream As Integer Dim sDeviceString As String Dim i As Integer, j As Integer Private Sub Form_Load() ' Create a WMEncoder object. Set Encoder = New WMEncoder ' Retrieve a device control plug-in info manager object from WMEncoder. Set DCPlugMgr = Encoder.DeviceControlPluginInfoManager ' Loop through the connected digital devices on the system such as DV cameras and VTRs. For i = 0 To DCPlugMgr.Count - 1 ' Set the IWMEncPluginInfo object to the current plug-in. Set PlugInfo = DCPlugMgr.Item(i) ' Find the first device plug-in that supports resources. If PlugInfo.SchemeType = "DeviceControl" And PlugInfo.Resources = True Then sDeviceString = PlugInfo.Item(0) Exit For End If Next i ' Add the device as the audio source and video source. Set SrcGrpColl = Encoder.SourceGroupCollection Set SrcGrp = SrcGrpColl.Add("SG_1") Set SrcAud = SrcGrp.AddSource(WMENC_AUDIO) Set SrcVid = SrcGrp.AddSource(WMENC_VIDEO) SrcAud.SetInput ("Device://" & sDeviceString) SrcVid.SetInput ("Device://" & sDeviceString) ' Encode to a file. Set File = Encoder.File File.LocalFileName = "C:\DeviceOutput.wmv" ' Select a profile from the collection and set it into the source group. Set ProColl = Encoder.ProfileCollection For i = 0 To ProColl.Count - 1 Set Pro = ProColl.Item(i) If (Pro.Name = "Windows Media Video 8 for Local Area Network (384 Kbps)") Then SrcGrp.Profile = Pro End If Next i ' Retrieve the device control collection, then add a device to it. Set DCColl = SrcGrp.DeviceControlCollection Set DControl = DCColl.Add DControl.SetInput ("DeviceControl://" & sDeviceString) ' Initialize the encoding session. Encoder.PrepareToEncode True ' Get the plug-in from the device. Set DCPlugin = DControl.GetDeviceControlPlugin ' Get the source plug-in for the pre-preview and then display it in the frame. Set PrePreview = SrcVid.GetSourcePlugin PrePreview.SetCaptureParent PreviewFrame.hWnd ' Retrieve the preview collection and create a preview object. Set DVColl_Preview = SrcVid.PreviewCollection Set Preview = New WMEncDataView End Sub Private Sub btnEncode_Click() ' Specify the stream to preview. lPreviewStream = DVColl_Preview.Add(Preview) ' Disable the VCR buttons. btnREW.Enabled = False btnPLAY.Enabled = False btnFF.Enabled = False btnSTOP.Enabled = False ' Start encoding. Encoder.Start ' Display the preview in PreviewFrame. Preview.SetViewProperties lPreviewStream, PreviewFrame.hWnd Preview.StartView (lPreviewStream) End Sub Private Sub btnREW_Click() ' Rewind. DCPlugin.SetOperation (WMENC_DEVICE_REW) End Sub Private Sub btnPLAY_Click() ' Play. DCPlugin.SetOperation (WMENC_DEVICE_PLAY) End Sub Private Sub btnFF_Click() ' Forward. DCPlugin.SetOperation (WMENC_DEVICE_FF) End Sub Private Sub btnSTOP_Click() ' Stop. DCPlugin.SetOperation (WMENC_DEVICE_STOP) End Sub Private Sub Encoder_OnDeviceControlStateChange(ByVal EnumState As WMEncoderLib.WMENC_DEVICECONTROL_STATE, ByVal sName As String, ByVal sScheme As String) ' When the device state changes, display the state in Label1. Select Case EnumState: Case WMENC_DEVICECONTROL_PLAYING Label1.Caption = "Playing" Case WMENC_DEVICECONTROL_STOPPED Label1.Caption = "Stopped" Case WMENC_DEVICECONTROL_FASTFORWARDING Label1.Caption = "Forwarding" Case WMENC_DEVICECONTROL_REWINDING Label1.Caption = "Rewinding" Case WMENC_DEVICECONTROL_UNSTABLE Label1.Caption = "Unstable" Case WMENC_DEVICECONTROL_EJECT Label1.Caption = "Eject" Case WMENC_DEVICECONTROL_ENDOFTAPE Label1.Caption = "End of tape" bDone = True End Select End Sub




Reply With Quote