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 ]
var netConn:NetConnection = new NetConnection();

netConn.connect(null);

var netStream:NetStream = new NetStream(netConn);

my_video.attachVideo(netStream);
netStream.setBufferTime(0);
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# ]
f_in_box__control1.AxCode.OnLoadExternalResourceByFullPath += 
   new f_in_box__lib.AxCode.
      OnLoadExternalResourceByFullPathEventHandler(OnLoadExternalResourceByFullPath);
...

private void OnLoadExternalResourceByFullPath(
   object sender, 
   String URL, 
   System.IO.Stream Stream, 
   ref bool Handled)
{
   if (URL == "http://FLV/FlashVideo.flv")
   {
      System.IO.Stream FLVStream = 
         this.GetType().Assembly.
            GetManifestResourceStream("Sample2_SWF_FLV_Embedding.Embedded_Movies.flashvideo.flv");

      const int size = 64 * 1024;
      byte[] buffer = new byte[size];
      int ReadBytes;

      while ( (ReadBytes = FromStream.Read(buffer, 0, size)) > 0 )
      {
         try
         {
            Stream.Write(buffer, 0, size);
         }
         catch (System.IO.IOException e)
         {
            // Loading is interrupted
            break;
         }
      }

      Stream.Close();

      Handled = true;
   }
}

[ VB.Net ]
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("Sample2_SWF_FLV_Embedding.flashvideo.flv")

      ' You can write all content right here, but if FLVStream 
      ' is BIG it takes long time
      ' The form will be unaccessible for user all this time
      ' Another option is to save Stream and write content in a 
      ' separate thread
      ' You can find code example in the sample Sample1_SWF_And_FLV_Player

      Const nSize = 64 * 1024

      Dim buffer(nSize) As Byte

      Dim nReadBytes As Integer

      While (True)
            nReadBytes = FLVStream.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

      Stream.Close()

      Handled = True
   End If
End Sub


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