ActiveCompanionSet xtras for Macromedia DirectorUsing 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."
halt
end if
if offset( "MediaPlayer.MediaPlayer", sprite(spriteNum).ProgId ) <> 1 then
alert "Bad control ProgId at sprite" && spriteNum && ", 'MediaPlayer.MediaPlayer.1' expected."
halt
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
mControl.Open(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
else
member("StatusText").text = "Something wrong: " & sprite(spriteNum).lastError
end if
end
on endSprite me
me.unprepareStepFrame()
if objectP(mControl) then
-- Unsubscribe from events
mControl.EventsHandler = VOID
end if
end
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
else
member("PositionText").text = ""
end if
end
on PrepareStepFrame me
set pos=getPos(the actorList,me)
if pos<=0 then add the actorList,me
end
on UnprepareStepFrame me
deleteOne the actorList,me
end
-- 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
end
-- 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
else
me.Error()
exit
end if
else if args[#NewState] = mControl.GetEnum(#mpStopped) and args[#OldState] = mControl.GetEnum(#mpPlaying) then
member("StatusText").text = "Stopped"
end if
end
-- MediaPlayer event that is set when something is wrong
on Error me, args
gVideoFailed = true
member("StatusText").text = "Something is wrong:" & string(args)
end
-- 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
end
-- 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
end
-- Used by buttons attached to this behavior
-- Returns the state for buttons
on GetStateFor me, symCmd
case symCmd of
#DoLoad:
return objectP(mControl)
#DoSwitchControls:
return objectP(mControl) and sprite(spriteNum).width > 0
end case
return false
end
-- 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
mControl.Open(strPath)
if mControl.Failed then
alert mControl.LastError
exit
end if
sendAllSprites #StateChanged
end
-- Called by button
-- Switchs MediaPLayer controls on or off
on DoSwitchControls me
if mControl.ShowControls then
mControl.ShowControls = false
else
mControl.ShowControls = true
end if
end
-- 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.InsertActiveX("MediaPlayer.MediaPlayer.1")
mem.name = "MediaPlayer"
end
-- 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
videoList.addAt(1,"")
videoList.addAt(1,"")
p_list = [:]
p_list[#mVideoMember] = [ \
#comment: "Video to play:",\
#format: #string,\
#range:videoList,\
#default:videoList[1]]\
return p_list
end
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.
|