Replace Audio using IltmmMultiStreamSource and IltmmMultiStreamTarget Example for Visual Basic
' *******************************************
' * This sample will join Video file and Wave File *
' * In one file *
' '*******************************************
Private Sub Form_Load()
Dim VideoTarget As ltmmSampleTarget
Dim AudioTarget As ltmmSampleTarget
Dim VideoAndAudioSource As ltmmMultiStreamSource
Set VideoAndAudioSource = New ltmmMultiStreamSource
VideoAndAudioSource.StreamCount = 2
Form1.Caption = "Prepare the video control..."
' Prepare VideoCtrl and set its source file to VideoFile.
' Convert only the video stream
' Create a new VideoTarget object and set as the sample target object in VideoCtrl
' Set the stream media type for pin 0 of VideoAndAudioSource to be the same as the video media type
' Also starts the conversion in VideoCtrl
PrepareSampleAndTarget VideoCtrl, "c:\Video.avi", ltmmMEDIATYPE_Video, VideoTarget, VideoAndAudioSource, 0
Form1.Caption = "Prepare the audio control..."
' Prepare AudioCtrl and set its source file to AudioFile.
' Convert only the audio stream
' Create a new AudioTarget object and set as the sample target object in AudioCtrl
' Set the stream media type for pin 1 of VideoAndAudioSource to be the same as the audio media type
' Also starts the conversion in AudioCtrl
PrepareSampleAndTarget AudioCtrl, "C:\Voice.wav", ltmmMEDIATYPE_Audio, AudioTarget, VideoAndAudioSource, 1
Form1.Caption = "Prepare the destination control..."
' prepare the destination file
DestCtrl.SourceObject = VideoAndAudioSource
DestCtrl.TargetFormat = ltmmConvert_TargetFormat_Avi
DestCtrl.TargetFile = "C:\Video_And_Voice.avi"
DestCtrl.StartConvert
Form1.Caption = "Copy the video samples..."
' Copy all the video samples from the Video file to the destination file
CopySamples VideoCtrl, VideoTarget, VideoAndAudioSource, 0
Form1.Caption = "Copy the audio samples..."
' Copy all the audio samples from the Audio file to the destination file
CopySamples AudioCtrl, AudioTarget, VideoAndAudioSource, 1
' wait for the destination control to stop the conversion
Do While (DestCtrl.State = ltmmConvert_State_Running)
DoEvents
Loop
DestCtrl.ResetSource
Form1.Caption = "Done."
End Sub
Private Sub PrepareSampleAndTarget(ByRef SourceCtrl As ltmmConvertCtrl, ByVal SourceFile As String, ByVal mtType As String, ByRef SampleTarget As ltmmSampleTarget, ByRef VideoAndAudioSource As ltmmMultiStreamSource, Stream As Long)
Dim InsertedMeidaType As ltmmMediaType
SourceCtrl.SourceFile = SourceFile
' allocate the SampleTarget object
Set SampleTarget = New ltmmSampleTarget
' create a ltmmMediaType and indicate the accepted type
Dim mt As ltmmMediaType
Set mt = New ltmmMediaType
mt.Type = mtType
' make the SampleTarget object accept connections of this media type
SampleTarget.SetAcceptedMediaType mt
' free the ltmmMediaType object
Set mt = Nothing
' set the output target object to be sample target
SourceCtrl.TargetObject = SampleTarget
' start the conversion, so I know the exact media type
SourceCtrl.StartConvert
' set the media type for the source stream to be the same as the media type in the input file (video or audio)
VideoAndAudioSource.SetMediaType Stream, SampleTarget.GetConnectedMediaType
‘ get the inserted media type for the first stream
Set InsertedMeidaType = VideoAndAudioSource.GetMediaType(0)
End Sub
Private Sub CopySamples(ByRef SourceCtrl As ltmmConvertCtrl, ByRef SampleTarget As ltmmSampleTarget, ByRef VideoAndAudioSource As ltmmMultiStreamSource, ByVal Stream As Long)
Dim msSrc As ltmmMediaSample
Dim msDst As ltmmMediaSample
Dim StartTimeHi As Long
Dim StartTimeLo As Long
Dim StopTimeHi As Long
Dim StopTimeLo As Long
Do
On Error GoTo EndConvert
Set msSrc = SampleTarget.GetSample(2000)
' get a source sample
Set msDst = VideoAndAudioSource.GetSampleBuffer(Stream, 2000)
' copy the data to the source sample
msDst.SetData msSrc.ActualDataLength, msSrc.Buffer
' copy the sample time
On Error GoTo ResetTime
msSrc.GetTime StartTimeHi, StartTimeLo, StopTimeHi, StopTimeLo
msDst.SetTime StartTimeHi, StartTimeLo, StopTimeHi, StopTimeLo
GoTo TimeIsSet
ResetTime:
msDst.ResetTime
TimeIsSet:
' copy the other flags
msDst.Discontinuity = msSrc.Discontinuity
msDst.Preroll = msSrc.Preroll
msDst.SyncPoint = msSrc.SyncPoint
' release the source sample
Set msSrc = Nothing
' deliver the source sample
VideoAndAudioSource.DeliverSample Stream, 2000, msDst
' release the source sample
Set msDst = Nothing
Loop While (1)
EndConvert:
' stop the conversion in the source control
SourceCtrl.StopConvert
' deliver end of stream for the current source stream
VideoAndAudioSource.DeliverEndOfStream Stream, 2000
End Sub