F-IN-BOX .NET Help >> How to

How to play Flash Video (FLV) from stream

Using F-IN-BOX you are able to play Flash Video (FLV) from external files, URL or directly from a stream. When F-IN-BOX loads Flash Video no temporary files are created everything runs directly from memory. You can encrypt your video and put into application's resource - F-IN-BOX loads FLV without ever saving or extracting the file to disk.

To play Flash Video from stream you should create flash movie that loads Flash Video from "private" URL (http://FLV/FlashVideo.flv). Flash Movie uses the following code to load Flash Video:

[ ActionScript ]
  1. var netConn:NetConnection = new NetConnection();  
  2.   
  3. netConn.connect(null);  
  4.   
  5. var netStream:NetStream = new NetStream(netConn);  
  6.   
  7. my_video.attachVideo(netStream);  
  8. netStream.setBufferTime(0);  
  9. netStream.play("http://FLV/FlashVideo.flv");  

When Flash tries to load Flash Video from http://FLV/FlashVideo.flv, F-IN-BOX provides content of FLV. Use the event OnLoadExternalResourceByFullPath to set handle the external resources and provide them to Flash. See the code:

[ C# ]
  1. f_in_box__control1.AxCode.OnLoadExternalResourceByFullPath +=   
  2.    new f_in_box__lib.AxCode.  
  3.       OnLoadExternalResourceByFullPathEventHandler(OnLoadExternalResourceByFullPath);  
  4. ...  
  5.   
  6. private void OnLoadExternalResourceByFullPath(  
  7.    object sender,   
  8.    String URL,   
  9.    System.IO.Stream Stream,   
  10.    ref bool Handled)  
  11. {  
  12.    if (URL == "http://FLV/FlashVideo.flv")  
  13.    {  
  14.       System.IO.Stream FLVStream =   
  15.          this.GetType().Assembly.  
  16.             GetManifestResourceStream("Sample2_SWF_FLV_Embedding.Embedded_Movies.flashvideo.flv");  
  17.   
  18.       const int size = 64 * 1024;  
  19.       byte[] buffer = new byte[size];  
  20.       int ReadBytes;  
  21.   
  22.       while ( (ReadBytes = FromStream.Read(buffer, 0, size)) > 0 )  
  23.       {  
  24.          try  
  25.          {  
  26.             Stream.Write(buffer, 0, size);  
  27.          }  
  28.          catch (System.IO.IOException e)  
  29.          {  
  30.             // Loading is interrupted  
  31.             break;  
  32.          }  
  33.       }  
  34.   
  35.       Stream.Close();  
  36.   
  37.       Handled = true;  
  38.    }  
  39. }  

[ VB.Net ]
  1. Private Sub OnLoadExternalResourceByFullPath(_  
  2.    ByVal sender As Object, _  
  3.    ByVal URL As String, _  
  4.    ByVal Stream As System.IO.Stream, _  
  5.    ByRef Handled As Boolean _  
  6. )  
  7.    If URL = "http://FLV/FlashVideo.flv" Then  
  8.       Dim FLVStream As System.IO.Stream = _  
  9.          Me.GetType().Assembly. _  
  10.             GetManifestResourceStream("Sample2_SWF_FLV_Embedding.flashvideo.flv")  
  11.   
  12.       ' You can write all content right here, but if FLVStream   
  13.       ' is BIG it takes long time  
  14.       ' The form will be unaccessible for user all this time  
  15.       ' Another option is to save Stream and write content in a   
  16.       ' separate thread  
  17.       ' You can find code example in the sample Sample1_SWF_And_FLV_Player  
  18.   
  19.       Const nSize = 64 * 1024  
  20.   
  21.       Dim buffer(nSize) As Byte  
  22.   
  23.       Dim nReadBytes As Integer  
  24.   
  25.       While (True)  
  26.             nReadBytes = FLVStream.Read(buffer, 0, nSize)  
  27.   
  28.             If 0 = nReadBytes Then Exit While  
  29.   
  30.             Try  
  31.                 ToStream.Write(buffer, 0, nReadBytes)  
  32.             Catch e As System.IO.IOException  
  33.                 ' Loading is interrupted  
  34.                 Exit While  
  35.             End Try  
  36.   
  37.       End While  
  38.   
  39.       Stream.Close()  
  40.   
  41.       Handled = True  
  42.    End If  
  43. End Sub  


Copyright © Softanics. All rights reserved.
F-IN-BOX is a trademark of Softanics.
Macromedia and Shockwave Flash are trademarks of Adobe