Question : Play flv file in VB.NET Form

Hi I have movie trailer in FLV format (9MB) I would like to play it in VB.NET WIndows Form ...

Answer : Play flv file in VB.NET Form

There are resources to be found, like this CodeProject article, that describes what you need to do to display a FLV file.

Fun with C# and the Flash Player 8 External API
http://www.codeproject.com/KB/audio-video/flashexternalapi.aspx
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
Imports System 
Imports System.Drawing 
Imports System.Collections 
Imports System.ComponentModel 
Imports System.Windows.Forms 
Imports System.Data 
Imports System.IO 
Imports System.Xml 
Imports AxShockwaveFlashObjects 

Namespace Vml.FLVPlayer 
    '''  
    ''' Summary description for Form1. 
    '''  
    Public Class FLVPlayer 
        Inherits System.Windows.Forms.Form 
        Private statusbar As StatusBar 
        Private fileNameStatusBarPanel As StatusBarPanel 
        Private videoPlaceholder As System.Windows.Forms.Panel 
        Private openVideo As System.Windows.Forms.Button 
        Private openVideoDialog As System.Windows.Forms.OpenFileDialog 
        Private flashPlayer As AxShockwaveFlashObjects.AxShockwaveFlash 
        
        '''  
        ''' Required designer variable. 
        '''  
        Private components As System.ComponentModel.Container = Nothing 
        
        Public Sub New() 
            ' 
            ' Required for Windows Form Designer support 
            ' 
            InitializeComponent() 
            
            Try 
                flashPlayer.LoadMovie(0, Application.StartupPath & "\player.swf") 
                AddHandler flashPlayer.FlashCall, AddressOf flashPlayer_FlashCall 
            Catch ex As Exception 
                ExceptionUtilities.DisplayException("Unable to load SWF video player, please verify you have Flash Player 8 installed and try again.") 
                Me.Dispose() 
            End Try 
        End Sub 
        
        Public Sub New(ByVal moviePath As String) 
            Me.New() 
            Me.LoadVideo(moviePath) 
        End Sub 
        
        '''  
        ''' Clean up any resources being used. 
        '''  
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) 
            If disposing Then 
                If components IsNot Nothing Then 
                    components.Dispose() 
                End If 
            End If 
            MyBase.Dispose(disposing) 
        End Sub 
        
        #Region "Windows Form Designer generated code" 
        '''  
        ''' Required method for Designer support - do not modify 
        ''' the contents of this method with the code editor. 
        '''  
        Private Sub InitializeComponent() 
            Dim resources As New System.Resources.ResourceManager(GetType(FLVPlayer)) 
            Me.openVideo = New System.Windows.Forms.Button() 
            Me.openVideoDialog = New System.Windows.Forms.OpenFileDialog() 
            Me.statusbar = New System.Windows.Forms.StatusBar() 
            Me.fileNameStatusBarPanel = New System.Windows.Forms.StatusBarPanel() 
            Me.videoPlaceholder = New System.Windows.Forms.Panel() 
            Me.flashPlayer = New AxShockwaveFlashObjects.AxShockwaveFlash() 
            DirectCast((Me.fileNameStatusBarPanel), System.ComponentModel.ISupportInitialize).BeginInit() 
            Me.videoPlaceholder.SuspendLayout() 
            DirectCast((Me.flashPlayer), System.ComponentModel.ISupportInitialize).BeginInit() 
            Me.SuspendLayout() 
            ' 
            ' openVideo 
            ' 
            Me.openVideo.FlatStyle = System.Windows.Forms.FlatStyle.System 
            Me.openVideo.Location = New System.Drawing.Point(8, 272) 
            Me.openVideo.Name = "openVideo" 
            Me.openVideo.Size = New System.Drawing.Size(88, 23) 
            Me.openVideo.TabIndex = 1 
            Me.openVideo.Text = "Open..." 
            AddHandler Me.openVideo.Click, AddressOf Me.openVideo_Click 
            ' 
            ' statusbar 
            ' 
            Me.statusbar.Location = New System.Drawing.Point(0, 298) 
            Me.statusbar.Name = "statusbar" 
            Me.statusbar.Panels.AddRange(New System.Windows.Forms.StatusBarPanel() {Me.fileNameStatusBarPanel}) 
            Me.statusbar.ShowPanels = True 
            Me.statusbar.Size = New System.Drawing.Size(338, 22) 
            Me.statusbar.SizingGrip = False 
            Me.statusbar.TabIndex = 4 
            ' 
            ' fileNameStatusBarPanel 
            ' 
            Me.fileNameStatusBarPanel.Text = "File not loaded" 
            Me.fileNameStatusBarPanel.ToolTipText = "The file currently loaded" 
            Me.fileNameStatusBarPanel.Width = 810 
            ' 
            ' videoPlaceholder 
            ' 
            Me.videoPlaceholder.BackColor = System.Drawing.Color.Black 
            Me.videoPlaceholder.Controls.Add(Me.flashPlayer) 
            Me.videoPlaceholder.Location = New System.Drawing.Point(8, 8) 
            Me.videoPlaceholder.Name = "videoPlaceholder" 
            Me.videoPlaceholder.Size = New System.Drawing.Size(320, 240) 
            Me.videoPlaceholder.TabIndex = 6 
            ' 
            ' flashPlayer 
            ' 
            Me.flashPlayer.ContainingControl = Me 
            Me.flashPlayer.Enabled = True 
            Me.flashPlayer.Location = New System.Drawing.Point(0, 0) 
            Me.flashPlayer.Name = "flashPlayer" 
            Me.flashPlayer.OcxState = DirectCast((resources.GetObject("flashPlayer.OcxState")), System.Windows.Forms.AxHost.State) 
            Me.flashPlayer.Size = New System.Drawing.Size(40, 24) 
            Me.flashPlayer.TabIndex = 0 
            ' 
            ' FLVPlayer 
            ' 
            Me.AccessibleName = "FLVPlayer" 
            Me.AllowDrop = True 
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) 
            Me.ClientSize = New System.Drawing.Size(338, 320) 
            Me.Controls.Add(Me.statusbar) 
            Me.Controls.Add(Me.openVideo) 
            Me.Controls.Add(Me.videoPlaceholder) 
            Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle 
            Me.Icon = DirectCast((resources.GetObject("$this.Icon")), System.Drawing.Icon) 
            Me.MaximizeBox = False 
            Me.Name = "FLVPlayer" 
            Me.Text = "FLVPlayer" 
            AddHandler Me.DragDrop, AddressOf Me.FLVPlayer_DragDrop 
            AddHandler Me.DragEnter, AddressOf Me.FLVPlayer_DragEnter 
            DirectCast((Me.fileNameStatusBarPanel), System.ComponentModel.ISupportInitialize).EndInit() 
            Me.videoPlaceholder.ResumeLayout(False) 
            DirectCast((Me.flashPlayer), System.ComponentModel.ISupportInitialize).EndInit() 
                
            Me.ResumeLayout(False) 
        End Sub 
        #End Region 
        
        Private Sub openVideo_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
            openVideoDialog = New OpenFileDialog() 
            openVideoDialog.Filter = "*.flv|*.flv" 
            openVideoDialog.Title = "Select a Flash Video file..." 
            openVideoDialog.Multiselect = False 
            openVideoDialog.RestoreDirectory = True 
            
            If openVideoDialog.ShowDialog() = DialogResult.OK Then 
                LoadVideo(openVideoDialog.FileName) 
            End If 
        End Sub 
        
        Private Sub LoadVideo(ByVal videoPath As String) 
            fileNameStatusBarPanel.Text = videoPath 
            flashPlayer.CallFunction("" & videoPath & "") 
        End Sub 
        
        Private Sub FLVPlayer_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) 
            Dim files As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String()) 
            
            If fileNameStatusBarPanel.Text <> files(0) Then 
                LoadVideo(files(0)) 
            End If 
        End Sub 
        
        Private Sub FLVPlayer_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) 
            If e.Data.GetDataPresent(DataFormats.FileDrop, False) Then 
                e.Effect = DragDropEffects.All 
            End If 
        End Sub 
        
        ' private void btnUrl_Click(object sender, System.EventArgs e) 
        ' { 
        ' UrlDialog urlForm = new UrlDialog(); 
        ' 
        ' if(urlForm.ShowDialog(this) == DialogResult.OK) 
        ' { 
        ' if(urlForm.Url != null && urlForm.Url.Length > 0) 
        ' { 
        ' LoadVideo(urlForm.Url); 
        ' } 
        ' } 
        ' } 
        
        Public Sub ResizePlayer(ByVal width As Integer, ByVal height As Integer) 
            flashPlayer.Width = width 
            flashPlayer.Height = height 
            videoPlaceholder.Width = width 
            videoPlaceholder.Height = height 
        End Sub 
        
        Private Sub flashPlayer_FlashCall(ByVal sender As Object, ByVal e As _IShockwaveFlashEvents_FlashCallEvent) 
            Dim document As New XmlDocument() 
            document.LoadXml(e.request) 
            
            ' Since I have only one call back I just grab the arguments and call 
            ' the function. This needs to be made much more flexible when there are 
            ' multiple call backs going back and forth 
            Dim list As XmlNodeList = document.GetElementsByTagName("arguments") 
            ResizePlayer(Convert.ToInt32(list(0).FirstChild.InnerText), Convert.ToInt32(list(0).ChildNodes(1).InnerText)) 
        End Sub 
    End Class 
End Namespace
Random Solutions  
 
programming4us programming4us