F-IN-BOX Delphi Edition Help

Features

Load flash movies from any TStream descendant (TResourceStream, TMemoryStream, etc.)

It is known fact that the Flash Player ActiveX is able to load movies at certain URLs only. You have to save the movie to a temporary location from your application's resource before you can load it. You'll have to generate a corresponding link that you can pass to the Flash Player ActiveX and then delete the movie after. You'll have to admit that this is extremely inconvenient. Apart from being inconvenient there is a chance you'll fail when you try to create a temporary file if you don't have the appropriate permission or don't have access to a temporary folder. It is definitely not an appropriate approach when security is an issue since your movie can be easily intercepted. There is a solution! TFlashPlayerControl uses a different approach of swflash.ocx/flash.ocx code loading. It is able to load your movies directly to the ActiveX thereby avoiding the temporary file step. Load flash movies from any TStream descendant (TResourceStream, TMemoryStream, etc.)! The advantages are pretty obvious. And you can also protect your movies from unauthorized access using your favourite software protection application.

Using the component you can load any flash movie from any stream object. Just use the LoadMovieFromStream and PuMovieFromStream methods. No temporary files! Load any flash movie on-the-fly from any supported source. For example, you can put one or more flash movies in the resource section of your application and then load it from the exe! That's the portability and power of TFlashPlayerControl!

Here's an example of how to load a flash movie from a resource:

[ Delphi ]
{$RESOURCE 'res\movie.res'}
...

type
  TMainForm = class(TForm)
    FlashPlayerControl1: TFlashPlayerControl;
...

  end;
...

procedure TMainForm.FormCreate(Sender: TObject);
var
  ResourceStream: TResourceStream;
begin
  ResourceStream := TResourceStream.Create(0, 'EmbeddedMovie', 'FLASH');
  FlashPlayerControl1.PutMovieFromStream(ResourceStream);
  ResourceStream.Free;
end;

[ Builder C++ ]
#pragma resource "res\\movie.res"
...

class TMainForm : public TForm
{
__published:
        void __fastcall FormCreate(TObject *Sender);
...

};
...

void __fastcall TMainForm::FormCreate(TObject *Sender)
{
  TResourceStream* ResourceStream = new TResourceStream(0, "EmbeddedMovie", "FLASH");
  FlashPlayerControl1->PutMovieFromStream(ResourceStream);
  delete ResourceStream;
}
Back to TopBack to Top

Create flash-enabled applications which are ready to work even when the Macromedia / Adobe Flash Player ActiveX is not installed!

One of the biggest problem using the Flash Player ActiveX is the mandatory component registration. The common approach is to save swflash.ocx/flash.ocx code to temporary files and then register them. The disadvantages are the same as above mentioned - insufficient permissions to save and register an swflash.ocx/flash.ocx. Now you can forget about these problems! TFlashPlayerControl is able to use swflash.ocx/flash.ocx from any source. For example, you can put an swflash.ocx/flash.ocx code inside of your application's resources and instruct TFlashPlayerControl to use it. It is important to note that TFlashPlayerControl does not use temporary files and swflash.ocx/flash.ocx registration but loads and uses the code directly. No more user management rights problems - no more temporary files and no more component registrations. It is up to you to decide what swflash.ocx/flash.ocx to use - by default already registered component is used.

Traditionally, there are many obstacles or annoyances that one will encounter when using Macromedia / Adobe Flash Player ActiveX in a Delphi / Builder / VCL application.

The Application...

  1. needs swflash.ocx/flash.ocx installed in the system before it will work.
  2. has to work flawlessly with the already installed version of the Macromedia / Adobe Flash Player ActiveX.
  3. has no easy way to prevent flash movies from being abused.

TFlashPlayerControl to the rescue. It solves these problems and more!

By default, the component will use the swflash.ocx/flash.ocx that's already installed on the system. The component can alternatively use any swflash.ocx/flash.ocx that you would like to provide it with using any supported source. An example of this how you can embed the flash.ocx into the resource section of your application's exe file and then load it at runtime. Using this method, your application will work even if Macromedia / Adobe Flash Player ActiveX doesn't exist on the target system. Just use FlashPlayerControl.LoadFlashOCXCodeFromStream method! With TFlashPlayerControl, hassling around with Macromedia / Adobe Flash Player ActiveX installation issues are a thing the past! It's so easy to do! There are even demos provided which come complete with source code to prove it!

Here's an example of how to load swflash.ocx/flash.ocx code from a resource:

[ Delphi ]
{$RESOURCE 'res\flash.res'}
...

var
  FlashCodeStream: TResourceStream;


initialization
  FlashCodeStream := TResourceStream.Create(0, 'FlashOCXCode', 'BIN');
  FlashPlayerControl.LoadFlashOCXCodeFromStream(FlashCodeStream);
  FlashCodeStream.Free;

[ Builder C++ ]
#pragma resource "res\\flash_ocx.res"
...

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
...

  Application->Initialize();

  TResourceStream* FlashCodeStream = new TResourceStream(0, "FlashOCXCode", "BIN");
  Flashplayercontrol::LoadFlashOCXCodeFromStream(FlashCodeStream);
  delete FlashCodeStream;
...

}

Here's an example of how to load swflash.ocx/flash.ocx code from a file:

[ Delphi ]
var
  FlashCodeStream: TFileStream;


initialization
  FlashCodeStream := TFileStream.Create('flash.ocx', fmOpenRead or fmShareDenyNone);
  FlashPlayerControl.LoadFlashOCXCodeFromStream(FlashCodeStream);
  FlashCodeStream.Free;

[ Builder C++ ]
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
...

  Application->Initialize();

  TFileStream* FlashCodeStream = new TFileStream("flash.ocx", fmOpenRead | fmShareDenyNone);
  Flashplayercontrol::LoadFlashOCXCodeFromStream(FlashCodeStream);
  delete FlashCodeStream;
...

}
Back to TopBack to Top

Transparency is fully supported

Using FlashPlayerControl you are able to create applications based on transparent flash movies. You can create applications with translucency non-rectangle forms (windows). Use Flash to make applications with modern user interface, make a business logic using Delphi or Builder.

Use TTransparentFlashPlayerControl Component to work with transparency.

  1. Put TTransparentFlashPlayerControl on a form.
  2. Set MakeParentTransparent property to True. That's all!

Please note that form transparency is supported only under Win2k (or higher), 16 / 32 bpp display mode. To check if the transparency is supported use global function IsFormTransparentAvailable:

[ Delphi ]
if Not IsFormTransparentAvailable then
begin
  ShowMessage('Translucency based on Flash is not available');
  Application.Terminate;
end;

[ Builder C++ ]
if (!Flashplayercontrol::IsFormTransparentAvailable())
{
  ShowMessage("Translucency based on Flash is not available");
  Application->Terminate();
}
Back to TopBack to Top

Ability to play Flash Video (FLV) from stream

Using FlashPlayerControl you are able to play Flash Video (FLV) from external files, URL or directly from a TStream. When TFlashPlayerControl loads Flash Video no temporary files are created everything runs directly from memory. You can encrypt your video and put into application's resource - TFlashPlayerControl 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 (put this in under a button in your Swf Flash Movie):

[ 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, TFlashPlayerControl provides content of FLV. Use global procedure SetGlobalOnLoadExternalResourceHandler to set handle the external resources and provide them to Flash. See the code:

[ Delphi ]
type
  TMainForm = class(TForm)
    procedure FormCreate(Sender: TObject);
...

  private
    procedure OnGlobalLoadExternalResource(const URL: WideString; Stream: TStream);
...

end;
...

procedure TMainForm.FormCreate(Sender: TObject);
begin
    SetGlobalOnLoadExternalResourceHandler(OnGlobalLoadExternalResource);
end;
...

procedure TMainForm.OnGlobalLoadExternalResource(const URL: WideString; Stream: TStream);
var
  ResourceStream: TResourceStream;
begin
    if URL = 'http://FLV/FlashVideo.flv' then
    begin
        ResourceStream := TResourceStream.Create(0, 'FlashVideo', 'FLV');
        ResourceStream.SaveToStream(Stream);
        ResourceStream.Free;
    end;
end;

[ Builder C++ ]
class TMainForm : public TForm
{
__published:
        void __fastcall FormCreate(TObject *Sender);
...

private:
        void __fastcall OnGlobalLoadExternalResource(const WideString URL, 
                          Classes::TStream* Stream);
...

}
...

void __fastcall TMainForm::FormCreate(TObject *Sender)
{
  SetGlobalOnLoadExternalResourceHandler(OnGlobalLoadExternalResource);
}
...

void __fastcall TMainForm::OnGlobalLoadExternalResource(const WideString URL, 
   Classes::TStream* Stream)
{
  if (URL == WideString("http://FLV/FlashVideo.flv"))
  {
    TResourceStream* ResourceStream = new TResourceStream(0, "FlashVideo", "FLV");
    ResourceStream->SaveToStream(Stream);
    delete ResourceStream;
  }
}
Back to TopBack to Top

Enable/disable flash sounds

Using the component you can turn on/off all sounds in all loaded flash movies.

  1. Use global procedure SetAudioEnabled to enable/disable the sounds.
  2. Use GetAudioEnabled to get current status of sounds enabling.

[ Delphi ]
procedure TMainForm.Mute;
begin
  if FlashPlayerControl.GetAudioEnabled then
    FlashPlayerControl.SetAudioEnabled(False);
end;

[ Builder C++ ]
void __fastcall TMainForm::Mute()
{
  if (Flashplayercontrol::GetAudioEnabled())
    Flashplayercontrol::SetAudioEnabled(false);
}
Back to TopBack to Top

Adjust sound volume

You can set sound volume. Use the global procedure FlashPlayerControl.SetAudioVolume with a value 0 - 100 to set audio volume. Use FlashPlayerControl.GetAudioVolume to get current audio volume.

[ Delphi ]
FlashPlayerControl.SetAudioVolume(TrackBarSoundVolume.Position);

[ Builder C++ ]
Flashplayercontrol::SetAudioVolume(TrackBarSoundVolume->Position);
Back to TopBack to Top

Get a snap image of the current flash movie frame

You can get a bitmap images from the current flash movie. It means you are able create applications that can coverts Flash movies to a series of bitmaps, JPEGs and others. Also you can build generated images to make an AVI video for example.

[ Delphi ]
procedure TMainForm.ButtonSaveAsBitmapClick(Sender: TObject);
var
  Bitmap: TBitmap;
  Picture: TPicture;
begin
  Picture := TPicture.Create;
  Bitmap := FlashPlayerControl1.CreateFrameBitmap;
  Picture.Bitmap := Bitmap;

  Picture.SaveToFile('Frame.bmp');

  Bitmap.Free;
  Picture.Free;
end;

[ Builder C++ ]
void __fastcall TMainForm::ButtonSaveAsBitmapClick(TObject *Sender)
{
  TPicture* Picture = new TPicture;
  Graphics::TBitmap* Bitmap = FlashPlayerControl1->CreateFrameBitmap();
  Picture->Bitmap = Bitmap;

  Picture->SaveToFile("Frame.bmp");

  delete Bitmap;
  delete Picture;
}
Back to TopBack to Top

External API

F-IN-BOX supports External API. You can call functions of a movie and a movie is able to get data from an application synchronously (instead of fscommand).

Call an ActionScript function from an application

Register your function using ExternalInterface.addCallback:

[ ActionScript ]
import flash.external.*; 

ExternalInterface.addCallback("CallMeFromApplication", this, InternalFunction); 

function InternalFunction(str: String): String { 
   TextArea1.text = str; 
   return "The function was called successfully"; 
}

Use method CallFunction:

[ Delphi ]
var
   Response: WideString; 
begin
   Response := FlashPlayerControl1.CallFunction('Some text for TFlashPlayerControl'); 

   ShowMessage(Format('The function returned: %s', [ Response ] )); 
end;

[ Builder C++ ]
WideString Response = 
   FlashPlayerControl1->CallFunction("Some text for TFlashPlayerControl"); 

ShowMessage("The function returned: " + Response);

Call an application function from a flash script

Use flash.external.ExternalInterface.call::

[ ActionScript ]
on (click) { 
   _root.TextArea1.text = flash.external.ExternalInterface.call("SomeFunction"); 
}

Handle the event OnFlashCall:

[ Delphi ]
procedure TMainForm.FlashPlayerControl1FlashCall(ASender: TObject; 
  const request: WideString); 
begin
   FlashPlayerControl1.SetReturnValue('Current time is: ' + 
                                      TimeToStr(Time) + #13#10 + 
                                      'This info is returned by application');
end;

[ Builder C++ ]
void __fastcall TMainForm::FlashPlayerControl1FlashCall(TObject *ASender, 
      const WideString request) 
{ 
   FlashPlayerControl1->SetReturnValue("Current time is: " + 
                                       TimeToStr(Time()) + 
                                       "\r\nThis info is returned by application"); 
}
Back to TopBack to Top

Write code which is compatible with any version of Macromedia / Adobe Flash Player ActiveX (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)

One of the problem with Macromedia / Adobe Flash Player ActiveX programming is that you have to control what version of Macromedia / Adobe Flash Player ActiveX you are using. For example, the property "Stacking" exists only in Macromedia / Adobe Flash Player ActiveX 5 but doesn't exist in later revisions. TFlashPlayerControl automatically detects what Macromedia / Adobe Flash Player ActiveX version is being used and prevents failure if access to non existant properties/methods is attempted. Applications using TFlashPlayerControl are not only compatible with Macromedia / Adobe Flash Player ActiveX 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, but are also "smart" about how Macromedia / Adobe Flash Player ActiveX control is used. This makes your application more robust which can result in fewer technical support issues.

Back to TopBack to Top

Streaming

Streaming is an ability to load the content in the asynchronous mode. If you load a movie using LoadMovieFromStream, the movie is loaded into LoadMovieFromStream. If your movie is large, it may take some time. Another example: you have encoded an SWF/FLV file and want to load it into the player. If you use LoadMovieFromStream, the player will play the file only after all of the data is decoded. That's why the ability to provide content in the asynchronous mode is important.

Remarks:

  • to load a movie or another resource data (*.flv, *.jpg, *.mp3, *.xml etc.) you should use a stream (AStream). To provide content, write to this stream (AStream.Write);
  • if you write data in a separate thread, call AStream.Write using Synchronize;
  • if AStream.Write returns 0, loading has been cancelled (for example, another movie is being loaded);
  • free AStream (AStream.Free) when loading is finished (even if loading has been cancelled);

There are few use cases:

Loading a movie in the asynchronous mode.

Use LoadMovieUsingStream or PurMovieUsingStream.

Remarks:

  • use a movie stream to load a movie. To provide movie content, write to this stream;
  • if you write data in a separate thread, call Write (of the movie stream) using Synchronize;
  • if Write returns 0, loading has been cancelled (for example, another movie is being loaded);
  • free the movie stream when loading is finished (even if loading has been cancelled);

Here is an example how to load a movie:

[ Delphi ]
var
   MovieStream: TStream;
begin
   FlashPlayerControl1.LoadMovieUsingStream(0, MovieStream);
   ContentProviderThread := 
      TContentProviderThread.Create(MovieStream);
end;
...

TContentProviderThread = class(TThread)
private
   FMovieStream: TStream;
   FBuffer: TMemoryStream;
...

end;
...

procedure TContentProviderThread.DoWrite;
var
   nWrittenBytes: integer;
begin
   nWrittenBytes := FMovieStream.Write(FBuffer.Memory^, FBuffer.Size);

   if nWrittenBytes = 0 then
      // Write returns 0, if loading was cancelled by some reason
      FStop := true;
end;
...

procedure TContentProviderThread.Execute;
begin
   while not FStop do
   begin
      // preparing buffer
      PrepareBuffer;

      if FBuffer.Size > 0 then
         DoWrite;
   end;

   // free the stream after providing all amount of data!
   FMovieStream.Free;
end;

[ Builder C++ ]
TStream* MovieStream;

FlashPlayerControl1->LoadMovieUsingStream(0, MovieStream);

TContentProviderThread* ContentProviderThread = 
   new TContentProviderThread(MovieStream);
...

class TContentProviderThread : public TThread
{
   private:
      TStream* FMovieStream;
      TMemoryStream* FBuffer;
...

};
...

void __fastcall TContentProviderThread::DoWrite()
{
   int nWrittenBytes = FMovieStream->Write(FBuffer->Memory, FBuffer->Size);

   if (0 == nWrittenBytes)
      FStop = true;
}
...

void __fastcall TContentProviderThread::Execute()
{
   while (!FStop)
   {
      // preparing buffer
      PrepareBuffer();

      if (FBuffer->Size > 0)
         DoWrite();
   }

   // free the stream after providing all amount of data!
   FMovieStream->Free();
}

Loading an external resource (*.flv - flash video, *.swf, *.jpg, *.mp3, etc.) by full path (i.e. beginning with http://) in the asynchronous mode.

Instruction:

  1. set the global resource handler. Use the global procedure FlashPlayerControl.SetGlobalOnLoadExternalResourceHandlerEx;
  2. a movie loads a flash video from a "private" URL ( http://FLV/FlashVideo.flv);
  3. the handler is called. You compare the passed URL with "http://FLV/FlashVideo.flv". If they are equal, set bHandled to "true" and use the passed Stream to write the content of FLV;
  4. after all the data has been provided, free the stream;

Remarks:

  • you get a stream assigned with a resource in a handler. Set the handler using the global FlashPlayerControl.SetGlobalOnLoadExternalResourceHandlerEx procedure;
  • if you write data in a separate thread, call Write using Synchronize;
  • if Write returns 0, loading has been cancelled (for example, another movie is being loaded);
  • free the stream when loading is finished. Even if loading has been cancelled;

Here is an example:

[ Delphi ]
var
   MovieStream: TStream;
begin
   // set global handler
   SetGlobalOnLoadExternalResourceHandlerEx(ContentProvider);
   FlashPlayerControl1.LoadMovieFromStream(0, MovieStream); // load movie
...

procedure TMainForm.ContentProvider(const URL: WideString; 
                                    Stream: TStream; 
                                    out bHandled: Boolean);
var
   ContentProviderThread: TContentProviderThread;
begin
  if URL = 'http://FLV/FlashVideo.flv' then
  begin
    ContentProviderThread := TContentProviderThread.Create(Stream);

    bHandled := true;
  end;
end;
...

TContentProviderThread = class(TThread)
private
   FResourceStream: TStream;
   FBuffer: TMemoryStream;
...

end;
...

procedure TContentProviderThread.DoWrite;
var
   nWrittenBytes: integer;
begin
   nWrittenBytes := FResourceStream.Write(FBuffer.Memory^, FBuffer.Size);

   if nWrittenBytes = 0 then
      // Write returns 0, if loading was cancelled by some reason
      FStop := true;
end;
...

procedure TContentProviderThread.Execute;
begin
   while not FStop do
   begin
      // preparing buffer
      PrepareBuffer;

      if FBuffer.Size > 0 then
         DoWrite;
   end;

   // free the stream after providing all amount of data!
   FResourceStream.Free;
end;

[ Builder C++ ]
TStream* MovieStream;

FlashPlayerControl1->LoadMovieUsingStream(0, MovieStream);

TContentProviderThread* ContentProviderThread = 
   new TContentProviderThread(MovieStream);
...

class TContentProviderThread : public TThread
{
   private:
      TStream* FResourceStream;
      TMemoryStream* FBuffer;
...

};
...

void __fastcall TContentProviderThread::DoWrite()
{
   int nWrittenBytes = FResourceStream->Write(FBuffer->Memory, FBuffer->Size);

   if (0 == nWrittenBytes)
      FStop = true;
}
...

void __fastcall TContentProviderThread::Execute()
{
   while (!FStop)
   {
      // preparing buffer
      PrepareBuffer();

      if (FBuffer->Size > 0)
         DoWrite();
   }

   // free the stream after providing all amount of data!
   FResourceStream->Free();
}

A movie is loaded using LoadMovieFromStream, PutMovieFromStream, LoadMovieUsingStream or PutMovieUsingStream. A movie loads an external resource (*.swf, *.jpg, *.mp3, but except *.flv - flash video) by relative path (i.e. something like "images/image1.jpg"). You should provide the resource in the asynchronous mode.

In this case you should handle the event OnLoadExternalResourceEx.

Remarks:

  • you get stream assigned with a resource in a handler of the OnLoadExternalResourceEx event. Check the passed URL and if you are ready to provide the content, set bHandled, passed as a reference, to true, use the passed Stream to write the content;
  • if you write data in a separate thread, call Write using Synchronize;
  • if Write returns 0, loading has been cancelled (for example, another movie is being loaded). Don't forget to free the stream!
  • free the stream when all the content has been written;
  • Builder C++ only: sometimes Builder C++ creates OnLoadExternalResourceEx event handler with bool bHandled instead of bool& bHandled. If it happened, change bool to bool& in a header file and in a source file;

Here is an example how to provide content of an image:

[ Delphi ]
procedure TMainForm.FlashPlayerControl1LoadExternalResourceEx(
  ASender: TObject;
  const URL: WideString;
  Stream: TStream;
  var bHandled: Boolean);
var
   ContentProviderThread: TContentProviderThread;
begin
   if URL = 'images/embedded_image1.jpg' then
   begin
      ContentProviderThread := TContentProviderThread.Create(Stream);

      bHandled := true;
   end;
end;
...

TContentProviderThread = class(TThread)
private
   FResourceStream: TStream;
   FBuffer: TMemoryStream;
...

end;
...

procedure TContentProviderThread.DoWrite;
var
   nWrittenBytes: integer;
begin
   nWrittenBytes := FResourceStream.Write(FBuffer.Memory^, FBuffer.Size);

   if nWrittenBytes = 0 then
      // Write returns 0, if loading was cancelled by some reason
      FStop := true;
end;
...

procedure TContentProviderThread.Execute;
begin
   while not FStop do
   begin
      // preparing buffer
      PrepareBuffer;

      if FBuffer.Size > 0 then
         DoWrite;
   end;

   // free the stream after providing all amount of data!
   FResourceStream.Free;
end;

[ Builder C++ ]
void __fastcall TMainForm::FlashPlayerControl1LoadExternalResourceEx(
      TObject *ASender,
      const WideString URL,
      TStream *Stream,
      BooleanRef bHandled)
{
   if (URL == WideString("images/embedded_image1.jpg"))
   {
      TContentProviderThread* ContentProviderThread = 
         new TContentProviderThread(Stream);

      *bHandled = true;
   }
}
...

class TContentProviderThread : public TThread
{
   private:
      TStream* FResourceStream;
      TMemoryStream* FBuffer;
...

};
...

void __fastcall TContentProviderThread::DoWrite()
{
   int nWrittenBytes = FResourceStream->Write(FBuffer->Memory, FBuffer->Size);

   if (0 == nWrittenBytes)
      FStop = true;
}
...

void __fastcall TContentProviderThread::Execute()
{
   while (!FStop)
   {
      // preparing buffer
      PrepareBuffer();

      if (FBuffer->Size > 0)
         DoWrite();
   }

   // free the stream after providing all amount of data!
   FResourceStream->Free();
}
Back to TopBack to Top

More about TFlashPlayerControl
Copyright © Softanics. All rights reserved
F-IN-BOX is a registered trademark of Softanics|Delphi is a trademark of Borland Software Corporation|Macromedia and Shockwave Flash are trademarks of Adobe, Inc.