Public Class Form1

    Private Sub ButtonPlayFLV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPlayFLV.Click
        Dim control As Control
        control = sender
        contextMenuFLV.Show(control, New Point(0, control.Height))
    End Sub

    Private Sub buttonPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonPlay.Click
        f_in_box__control1.FlashMethod_CallFunction("<invoke name=" & """" & "resumeVideo" & """" & " returntype=" & """" & "void" & """" & "><arguments></arguments></invoke>")
    End Sub

    Private Sub buttonPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonPause.Click
        f_in_box__control1.FlashMethod_CallFunction("<invoke name=" & """" & "pauseVideo" & """" & " returntype=" & """" & "void" & """" & "><arguments></arguments></invoke>")
    End Sub

    Private Shared Function BuildFlashVars(ByVal TypeVideo As String, ByVal URL As String, ByVal Mute As Boolean, ByVal AudioVolume As Integer, ByVal AutoPlay As Boolean, ByVal ShowControls As Boolean) As String
        BuildFlashVars = "type_video=" + TypeVideo

        BuildFlashVars = BuildFlashVars + "&url=" + System.Web.HttpUtility.HtmlEncode(URL)

        ' Specify mute=1 to disable audio, don't specify mute at all to enable audio
        If Mute Then BuildFlashVars = BuildFlashVars + "&mute=1"

        BuildFlashVars = BuildFlashVars + "&volume=" + AudioVolume.ToString()

        BuildFlashVars = BuildFlashVars + "&auto_play="
        If AutoPlay Then
            BuildFlashVars = BuildFlashVars + "true"
        Else
            BuildFlashVars = BuildFlashVars + "false"
        End If

        BuildFlashVars = BuildFlashVars + "&hide_control="
        If ShowControls Then
            BuildFlashVars = BuildFlashVars + "false"
        Else
            BuildFlashVars = BuildFlashVars + "true"
        End If

    End Function

    Private Sub PlaySource(ByVal TypeVideo As String, ByVal URL As String)
        f_in_box__control1.FlashProperty_FlashVars = BuildFlashVars(TypeVideo, URL, Not CheckBoxEnableSounds.Checked, trackBarSoundVolume.Value, checkBoxPlayAutomatically.Checked, checkBoxShowControls.Checked)
        f_in_box__control1.PutMovieFromStream(Me.GetType().Assembly.GetManifestResourceStream("Sample08_Advanced_FLV_Player.FlashVideoPlayer.swf"))
    End Sub

    Private Sub playFLVFromFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles playFLVFromFileToolStripMenuItem.Click
        If openFileDialogFlashFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
            PlaySource("flv", openFileDialogFlashFile.FileName)
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler f_in_box__control1.AxCode.OnLoadExternalResourceByFullPath, AddressOf OnLoadExternalResourceByFullPath

        openFileDialogFlashFile.InitialDirectory = System.IO.Path.GetDirectoryName(Application.ExecutablePath)

        PlaySource("flv", "")

        f_in_box__control1.FlashProperty_AllowFullscreen = True
    End Sub

    Private Sub checkBoxShowControls_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkBoxShowControls.CheckedChanged
        If checkBoxShowControls.Checked Then
            f_in_box__control1.FlashMethod_CallFunction("<invoke name=" & """" & "showAllControl" & """" & " returntype=" & """" & "void" & """" & "><arguments></arguments></invoke>")
        Else
            f_in_box__control1.FlashMethod_CallFunction("<invoke name=" & """" & "hideAllControl" & """" & " returntype=" & """" & "void" & """" & "><arguments></arguments></invoke>")
        End If
    End Sub

    Private Function GetCurrentPosition() As Double
        Dim strXML As String
        strXML = f_in_box__control1.FlashMethod_CallFunction("<invoke name=" & """" & "getCurrPosition" & """" & "returntype=" & """" & "xml" & """" & "><arguments></arguments></invoke>")
        Dim doc1 As New Xml.XmlDocument
        doc1.LoadXml(strXML)
        Dim NumberFormatInfo As New System.Globalization.NumberFormatInfo
        NumberFormatInfo.NumberDecimalSeparator = "."
        GetCurrentPosition = Double.Parse(doc1.DocumentElement.InnerText, NumberFormatInfo)
    End Function

    Private Sub buttonBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonBack.Click
        Dim CurrentPosition As Double
        CurrentPosition = GetCurrentPosition()

        CurrentPosition = CurrentPosition - 5

        If CurrentPosition < 0 Then CurrentPosition = 0

        Dim CurrentPositionAsInteger As Integer
        CurrentPositionAsInteger = CurrentPosition

        f_in_box__control1.FlashMethod_CallFunction("<invoke name=" & """" & "seekVideo" & """" & " returntype=" & """" & "xml" & """" & "><arguments><number>" + CurrentPositionAsInteger.ToString() + "</number></arguments></invoke>")
    End Sub

    Private Sub playFLVEmbeddedInTheApplicationPlayDirectlyWithoutTemporaryFilesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles playFLVEmbeddedInTheApplicationPlayDirectlyWithoutTemporaryFilesToolStripMenuItem.Click
        PlaySource("flv", "http://FLV/FlashVideo.flv")
    End Sub

    Private Sub OnLoadExternalResourceByFullPath(ByVal sender As Object, ByVal URL As String, ByVal Stream As System.IO.Stream, ByRef Handled As Boolean)
        If URL = "http://FLV/FlashVideo.flv" Then
            Dim FLVStream As System.IO.Stream = Me.GetType().Assembly.GetManifestResourceStream("Sample08_Advanced_FLV_Player.flashvideo.flv")

            Dim FromStreamToStreamWriter As New FromStreamToStreamWriter(FLVStream, Stream)

            Handled = True
        End If
    End Sub

    Private Sub buttonForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonForward.Click
        Dim CurrentPosition As Double
        CurrentPosition = GetCurrentPosition()

        CurrentPosition = CurrentPosition + 5

        Dim CurrentPositionAsInteger As Integer
        CurrentPositionAsInteger = CurrentPosition

        f_in_box__control1.FlashMethod_CallFunction("<invoke name=" & """" & "seekVideo" & """" & " returntype=" & """" & "xml" & """" & "><arguments><number>" + CurrentPositionAsInteger.ToString() + "</number></arguments></invoke>")
    End Sub

    Private Sub timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles timer1.Elapsed
        Dim CurrentPosition As Double
        CurrentPosition = GetCurrentPosition()

        If CurrentPosition <> -1 Then
            Dim PositionInSeconds As Integer
            PositionInSeconds = CurrentPosition

            trackBarVideoPosition.Value = CurrentPosition

            Dim PositionMS As Integer
            PositionMS = (CurrentPosition * 100) Mod 100

            LabelInfo.Text = "Current time: " + PositionInSeconds.ToString() + " s " + PositionMS.ToString() + " ms"
        End If
    End Sub

    Private Sub CheckBoxEnableSounds_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxEnableSounds.CheckedChanged
        Dim value As String

        If CheckBoxEnableSounds.Checked Then
            value = "<false />"
        Else
            value = "<true />"
        End If

        f_in_box__control1.FlashMethod_CallFunction("<invoke name=" & """" & "setMute" & """" & " returntype=" & """" & "xml" & """" & "><arguments>" + value + "</arguments></invoke>")

        If CheckBoxEnableSounds.Checked Then
            labelSoundVolume.Text = trackBarSoundVolume.Value.ToString() + " %"
        Else
            labelSoundVolume.Text = "Disabled"
        End If

        trackBarSoundVolume.Enabled = CheckBoxEnableSounds.Checked
    End Sub

    Private Sub trackBarSoundVolume_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trackBarSoundVolume.Scroll
        f_in_box__control1.FlashMethod_CallFunction("<invoke name=" & """" & "setVolume" & """" & " returntype=" & """" & "xml" & """" & "><arguments><number>" + trackBarSoundVolume.Value.ToString() + "</number></arguments></invoke>")

        labelSoundVolume.Text = trackBarSoundVolume.Value.ToString() + " %"
    End Sub

    Private Sub playFLVFromURLToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles playFLVFromURLToolStripMenuItem.Click
        Dim FormURL As New FormURL

        If System.Windows.Forms.DialogResult.OK = FormURL.ShowDialog() Then
            PlaySource("flv", FormURL.URL)
        End If
    End Sub

    Private Sub playFLVFromRTMPSourceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles playFLVFromRTMPSourceToolStripMenuItem.Click
        Dim FormURL As New FormURL

        If System.Windows.Forms.DialogResult.OK = FormURL.ShowDialog() Then
            PlaySource("rtmp", FormURL.URL)
        End If
    End Sub

    Private Sub f_in_box__control1_OnFlashCall(ByVal sender As System.Object, ByVal request As System.String) Handles f_in_box__control1.OnFlashCall
        Dim f_in_box__control As f_in_box__lib.f_in_box__control = sender

        Dim doc As New Xml.XmlDocument
        doc.LoadXml(request)

        Dim strRequestName As String = doc.DocumentElement.Attributes("name").Value

        If strRequestName = "onMetaData" Then
            ' Metadata (i.e. information about video) available
            Dim strXMLDuration As String = f_in_box__control.FlashMethod_CallFunction("<invoke name=" & """" & "getDuration" & """" & " returntype=" & """" & "xml" & """" & "><arguments></arguments></invoke>")
            Dim doc1 As New Xml.XmlDocument
            doc1.LoadXml(strXMLDuration)
            Dim NumberFormatInfo As New System.Globalization.NumberFormatInfo
            NumberFormatInfo.NumberDecimalSeparator = "."
            Dim Duration As Double = Double.Parse(doc1.DocumentElement.InnerText, NumberFormatInfo)

            trackBarVideoPosition.Maximum = Duration
            trackBarVideoPosition.Minimum = 0
        ElseIf strRequestName = "onVideoStatus" Then
            Dim strVideoStatus As String = doc.DocumentElement.ChildNodes(0).ChildNodes(0).InnerText
        End If
    End Sub
End Class

Class FromStreamToStreamWriter
    Private FromStream As System.IO.Stream
    Private ToStream As System.IO.Stream
    Private Thread As System.Threading.Thread

    Public Sub New(ByVal FromStream As System.IO.Stream, ByVal ToStream As System.IO.Stream)
        Me.FromStream = FromStream
        Me.ToStream = ToStream

        Thread = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf Main))

        Thread.Start()
    End Sub

    Private Sub Main()
        Const nSize As Integer = 64 * 1024
        Dim buffer(nSize) As Byte

        Dim nReadBytes As Integer

        While (True)
            nReadBytes = FromStream.Read(buffer, 0, nSize)

            If 0 = nReadBytes Then Exit While

            Try
                ToStream.Write(buffer, 0, nReadBytes)
            Catch e As System.IO.IOException
                ' Loading is interrupted
                Exit While
            End Try

        End While

        FromStream.Close()
        ToStream.Close()
    End Sub
End Class
