A list of products available on this site
Documentation for XtraMania's xtras
Prices and links to the online store
Xtras, PDFs, samples
Have a question? Ask us!
Logo. www.xtramaina.com  
Home Search E-mail
VbScriptXtra Samples/MediaPlayer

ActiveCompanionSet xtras for Macromedia Director

Using InternetExplorer ActiveX control right on the Stage

This sample uses both ActiveX xtra and VbScriptXtra from ActiveCompanionSet.

ActiveX xtra from ActiveCompanionSet is similar to the native Director ActiveX support except one important thing. It provides advanced scripting support via VbScriptXtra.

For example ActiveCompanionSet allows using MediaPlayer ActiveX for on stage playing different media types from audio, video to DVD movies.

Below is the code of behavior that should be placed on MediaPlayer ActiveX sprite. This code relies on older MediaPlayer interface that was provided with Windows prior to XP. It allows smooth playing different media without installation of the latest MediaPlayer even on older Windows versions.

-- Sample MediaPlayer Controller behaviour

property spriteNum

-- Keeps the scripting interface of the MediaPlayer
-- Use put sprite(1).mControl.Interface() to see what you can do with it
property mControl

-- Initialized by behavior properties if you have linked video members in the cast lib
property mVideoMember

-- Global variable that is set to true if error has occured during playback
-- It allows detection whether MediaPlayer is able to play the video (whether it is able to find the codec)
global gVideoFailed

on beginSprite me
  if sprite(spriteNum).member.type <> #ActiveXControl then
    alert "Bad member type at sprite" && spriteNum && ", #ActiveXControl expected."
  end if
  if offset( "MediaPlayer.MediaPlayer", sprite(spriteNum).ProgId ) <> 1 then
    alert "Bad control ProgId at sprite" && spriteNum && ", 'MediaPlayer.MediaPlayer.1' expected."
  end if
  member("TitleText").text = ""
  member("StatusText").text = ""
  member("PositionText").text = ""
  mControl = sprite(spriteNum).GetObject()
  if objectP(mControl) then
    -- Subscribe to the events
    mControl.EventsHandler = me
    -- Set debug mode if you need it
    -- mControl.debugMode = true
    -- There is some initialization
    -- It is not needed here since these properties are saved with the cast member
    --mControl.showControls = #false
    --mControl.EnableContextMenu = #false
    --mControl.SendKeyboardEvents = #true
    --mControl.ClickToPlay = #false
    --mControl.volume = 0
    -- If we are inialized try to start video
    if mVideoMember "" then
      member("TitleText").text = member(mVideoMember).fileName
    end if
    -- These lines allow MediaPlayer to handle keyboard events
    -- Note that clicking the stage moves the keyboard focus to the stage (or one of its sprites)
    -- Setting sprite's focus property causes the focus to be moved on to MediaPlayer window
    the keyboardFocusSprite = spriteNum
    sprite(spriteNum).focus = true
    -- We use stepFrame event to update current position
    prepareStepFrame me
    member("StatusText").text = "Something wrong: " & sprite(spriteNum).lastError
  end if

on endSprite me
  if objectP(mControl) then
    -- Unsubscribe from events
    mControl.EventsHandler = VOID
  end if

on stepFrame me
  -- Current position is -1 when it is unknown
  if mControl.CurrentPosition > 0 then
    member("PositionText").text = string(mControl.CurrentPosition) & " of " & mControl.duration
    member("PositionText").text = ""
  end if

on PrepareStepFrame me
  set pos=getPos(the actorList,me)
  if pos<=0 then add the actorList,me

on UnprepareStepFrame me
  deleteOne the actorList,me

-- After we set mControl.EventsHandler = me
-- We will get this handler call on any event
-- Also specific event handler will be called to
on IncomingEvent me, event, args
  put event,args

-- MediaPlayer event that is set when play state is changed somehow
-- We use it to set the correct sprite rect by detecting the original movie size
-- If movie size cannot be determined then MediaPlayer probably has not found the video codec
-- although it can probably play audio stream from the video file
-- Also we may check here when movie has stopped
on PlayStateChange me, args
  global gVideoFailed
  member("StatusText").text = string(args)
  if args[#NewState] = mControl.GetEnum(#mpPlaying) then
    if mControl.ImageSourceWidth > 0 and mControl.ImageSourceHeight > 0 then
      gVideoFailed = false
      sprite(spriteNum).width = min( mControl.ImageSourceWidth, the stage.rect.width )
      sprite(spriteNum).height = min( mControl.ImageSourceHeight, the stage.rect.height - 80 )
      sendAllSprites #StateChanged
    end if
  else if args[#NewState] = mControl.GetEnum(#mpStopped) and args[#OldState] = mControl.GetEnum(#mpPlaying) then
    member("StatusText").text = "Stopped"
  end if

-- MediaPlayer event that is set when something is wrong
on Error me, args
  gVideoFailed = true
  member("StatusText").text = "Something is wrong:" & string(args)

-- We won't ever get Director's mouseDown, since mouse is actually intercepted by MediaPlayer window
-- But we will get mouseDown event that is set by ActiveX control (MediaPlayer)
on mouseDown me, args
  if not listP(args) then
    put "Sprite"&&spriteNum&&"on MouseDown"
  end if

-- We may get this event from both Director and ActiveX (args is property list in this case)
on KeyDown me, args
  if not listP(args) then
    put "Sprite"&&spriteNum&&"on KeyDown"
    sprite(spriteNum).focus = true
  end if

-- Used by buttons attached to this behavior
-- Returns the state for buttons
on GetStateFor me, symCmd
  case symCmd of
      return objectP(mControl)
      return objectP(mControl) and sprite(spriteNum).width > 0
  end case
  return false

-- Called by button
-- Shows OpenFileDialog to choose an audio or video file
-- FileXtra4 is needed here
on DoLoad me
  fx = xtra("FileXtra4").new()
  strPath = fx.fx_FileOpenDialog("", "Media files (*.*)/*.*", "Select Video, Audio or Image file", false, true)
  if strPath = "" then exit
  member("TitleText").text = strPath
  if mControl.Failed then
    alert mControl.LastError
  end if
  sendAllSprites #StateChanged

-- Called by button
-- Switchs MediaPLayer controls on or off
on DoSwitchControls me
  if mControl.ShowControls then
    mControl.ShowControls = false
    mControl.ShowControls = true
  end if

-- The sample code that creates the older MediaPlayer ActiveX control
-- It is not registered as a control actually, therefore it is not shown in Controls list (while new MediaPlayer is shown)
-- So you can create the ActiveX control member by script in this way:
on CreateMember me
  mem = new(#ActiveXControl)
  mem.name = "MediaPlayer"

-- It scans all castlibs for viseo member to show the valid list of video members
on getPropertyDescriptionList me
  set videoList=[]
  repeat with castLibNumber= 1 to the number of castLibs
    repeat with i= 1 to the number of members of castLib castLibNumber
      set mem=member i of castLib castLibNumber
      if the type of mem=#digitalVideo then
        add videoList,the name of mem
      end if
    end repeat  
  end repeat
  sort videoList
  p_list = [:]
  p_list[#mVideoMember] = [ \
       #comment:   "Video to play:",\
       #format:   #string,\

  return p_list  

In this sample mControl is usual VBScriptXtra wrapper. Refer to the VbScriptXtra's documentation for more details.

Sample Director movie is available to download. It allows on stage playing different media that can be played by Microsoft MediaPlayer.

Where to find more info about MediaPlayer?

VbScriptXtra provides autodocumentation feature allowing you to see what you can do with particular object instance. Just create a MediaPlayer object and type in Messages window:
put interface(mp)

ObjectBrowser xtra - autodocumentation companion for VbScriptXtra - will show all available MediaPlayer interfaces, methods, properties and enumerations.

ObjectBrowser xtra is a part of ActiveCompanionSet. It is available to Download.

Site homeSearchContact author © Eugene Shoustrov. www.xtramania.com