Add Mediaplayer (or video/audio) to RichView

General TRichView support forum. Please post your questions here
Post Reply
wvd_vegt
Posts: 83
Joined: Tue Aug 30, 2005 7:39 am

Add Mediaplayer (or video/audio) to RichView

Post by wvd_vegt »

Hi,

Is there samplecode of how to add audio and video to TRichViewEdit, preferably by the MediaPlayer so it can be rendered to a (x)html page.


wvd_vegt
Posts: 83
Joined: Tue Aug 30, 2005 7:39 am

Post by wvd_vegt »

Hi,

Thanks a lot, will check it out tommorrow!

wvd_vegt
Posts: 83
Joined: Tue Aug 30, 2005 7:39 am

Post by wvd_vegt »

Hi,

It works but i still have some minor problems

1) I added the following code just after the Player.open call to resize the player to the movie. But TRIchViewEdit keeps showing the cursor at the old size and position.

Code: Select all

            mw := Player.DisplayRect.Right - Player.DisplayRect.Left;
            mh := Player.DisplayRect.Bottom - Player.DisplayRect.Top;
            VideoPanel.Width := mw;
            VideoPanel.Height := mh;

            Panel.Width := mw;
            Panel.Height := mh + Player.Height;

            Player.Width := mw;
2) Addding a

Code: Select all

            Player.Play 
behind above code starts it playing directly after insertion (no black square and more like the swf sample).

3) When I delete the avi and try add it again without closing the editor I get a message that 'The device name is alreay being used as an alias by this application'. It also happens whan you insert a second copy of the same movie into a document.

4) When I insert a movie into a tablecell the cell is enlarged a bit but not to the size of the movie being inserted (maybe related to point 1, as the avi was larger then the default player size).

wvd_vegt
Posts: 83
Joined: Tue Aug 30, 2005 7:39 am

Post by wvd_vegt »

Hi,

I fixed points 1 and 4 of the previous reply by adding a

Code: Select all

Editor.Format;
just after the finally block.

Another point i forgot to mention is that if the code is used in an application with more than one TRichView the OnPlayerClick needs to know which editor it's addressing. The following code finds the editor and selects the video for resizing:

Code: Select all

procedure T_MainForm.OnPlayerClick(Sender: TObject);
var Panel           : TPanel;
begin
  Panel := Sender as TPanel;
  if Panel.Parent is TPanel then
    Panel := TPanel(Panel.Parent);
  if (Panel.Parent is TRichViewEdit) then
    (Panel.Parent as TRichViewEdit).SelectControl(Panel);
end;

wvd_vegt
Posts: 83
Joined: Tue Aug 30, 2005 7:39 am

Post by wvd_vegt »

Hi,

The following code centers the avi at it's original size and also supports audio.

Only problems left are:

1) to correctly enable the pause/stop button after the play command (Delphi problem) and

2) correctly size the mediaplayer that is emebbed in the exported htmlpage (as it has a different size than a TMediaplayer).

Code: Select all

procedure T_MainForm.rvActionVideoExecute(Sender: TObject;
  Editor: TCustomRichViewEdit);
var Panel, VideoPanel: TPanel;
  Player            : TMediaPlayer;
  mh                : Integer;
  mw                : Integer;
begin
  if OpenDialog2.Execute then
    begin
      // Creating the player panel
      Panel := TPanel.Create(nil);
      Panel.BevelInner := bvNone;
      Panel.BevelOuter := bvNone;
      Panel.Width := 200;

      // Creating a panel to show video
      VideoPanel := TPanel.Create(Panel);
      VideoPanel.Parent := Panel;
      VideoPanel.BevelInner := bvNone;
      VideoPanel.BevelOuter := bvNone;
      VideoPanel.Width := 200;
      VideoPanel.Height := 200;
      VideoPanel.Color := clBlack;

      // Creating a player control
      Player := TMediaPlayer.Create(Panel);
      Player.Parent := Panel;
      Player.ColoredButtons := [];
      Player.VisibleButtons := [btPlay, btPause, btStop];
      Player.Height := 20;
      Panel.Height := VideoPanel.Height + Player.Height;
      Player.Left := (Panel.Width - Player.Width) div 2;
      Player.Top := VideoPanel.Height;
      Player.Display := VideoPanel;

      Panel.OnResize := OnPlayerResize;
      Panel.OnClick := OnPlayerClick;
      VideoPanel.OnClick := OnPlayerClick;

      // Inserting in TRichViewEdit
      Editor.TopLevelEditor.BeginUndoGroup(rvutInsert);
      Editor.TopLevelEditor.SetUndoGroupMode(True);                             // grouping InsertControl & SetCurrentItemExtraIntProperty
      try
        if Editor.InsertControl('', Panel, rvvaBaseline) then
          begin
            Editor.SetCurrentItemExtraIntProperty(rvepResizable, 1, True);

            Player.FileName := OpenDialog2.FileName;
            Player.Open;

            mw := Player.DisplayRect.Right - Player.DisplayRect.Left;
            mh := Player.DisplayRect.Bottom - Player.DisplayRect.Top;

            VideoPanel.Width := mw;
            VideoPanel.Height := mh;
            VideoPanel.Left := (Panel.Width - mw) div 2;

            Panel.Width := Max(mw, Player.Width);
            Panel.Height := Max(mh, 0) + Player.Height;

            Player.Top := Panel.Height - Player.Height;
            Player.Play;
          end;
      finally
        Editor.TopLevelEditor.SetUndoGroupMode(False);
      end;
      Editor.Format;
    end;
end;
and

Code: Select all

procedure T_MainForm.OnPlayerResize(Sender: TObject);
var Panel, VideoPanel: TPanel;
  Player            : TMediaPlayer;
begin
  Panel := Sender as TPanel;
  VideoPanel := Panel.Components[0] as TPanel;
  Player := Panel.Components[1] as TMediaPlayer;

  VideoPanel.Left := (Panel.Width - VideoPanel.Width) div 2;
  VideoPanel.Top := ((Panel.Height - VideoPanel.Height - Player.Height) div 2);

  Player.Left := (Panel.Width - Player.Width) div 2;
  Player.Top := Panel.Height - Player.Height;
end;
and finally

Code: Select all

procedure T_MainForm.OnPlayerClick(Sender: TObject);
var Panel           : TPanel;
begin
  Panel := Sender as TPanel;
  if Panel.Parent is TPanel then
    Panel := TPanel(Panel.Parent);
  if (Panel.Parent is TRichViewEdit) then
    (Panel.Parent as TRichViewEdit).SelectControl(Panel);
end;

Sergey Tkachenko
Site Admin
Posts: 14899
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

About resizing: call ResizeCurrentControl(NewWidth, NewHeight) instead of Format, it's much more faster.
Or better yet, resize it properly before the insertion to avoid flickering (resizing may require that the panel's Parent is assigned; in this case, temporarily make it hidden (Panel.Visible := False) and assign its parent (Panel.Parent := RichViewEdit1))

Please find answers to other your questions yourself, since they are not directly related to TRichView.
I am afraid that the problem of playing the same file by several players cannot be solved using TMediaPlayer. I believe it can be solved by providing an unique alias name in TMCI_Open_Parms structure when opening video (procedure TMediaPlayer.Open), but TMediaPlayer does not allow to specify aliases.

Post Reply