Skip to content
Tags

, ,

Embedding Flash

July 14, 2008

On the home page of my site, I wanted to place an unspecified number of Flash “teasers”, each linking to somewhere in the site. I also needed to specify an image for each “teaser” to be used as a backup for users without Flash support.

This is how I did it…

My template for these “teasers” looks like this:

Teaser template

So, I have a Droplink which lets the user select a Flash movie from a specified folder in the Media Library, an Image which is also selected from a specified folder in the Media Library, and a General Link which determines where the teaser should link to.

In my content tree, I have a folder where I store “Teaser” objects:

Folder of teasers in the content tree

On my “Home Page” template, I use a Multilist which points to this folder, so I can manually select which ones will appear on the home page.

Selecting which teasers go on the home page

 So, that’s the content structure in place. Now for the presentation stuff.

I decided to use Adobe’s Flash Player Detection script to do the actual embedding (available from Adobe’s web site). Flash movies in Sitecore have a .ashx extension, so I had to modify the script slightly because it hard-codes the extension as .swf in the AC_FL_RunContent() function. Just change the second parameter of the call to AC_GetArgs from “.swf” to an empty string “”:

// ...
// ...
function AC_FL_RunContent(){
var ret =
AC_GetArgs
( arguments, "", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
, "application/x-shockwave-flash"
);
AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}
// ...
// ...

I then added a Javascript helper function which calls this function (passing the link path to the Flash movie as a FlashVars parameter), and outputs a linked image for users without appropriate Flash support.

var flashInstance = 0;
function doTeaser (swfPath, swfWidth, swfHeight, swfMajorVersion, swfMinorVersion, swfRevision, swfBgColor, imgPath, imgWidth, imgHeight, imgAlt, linkPath)
{
    var hasRequestedVersion = DetectFlashVer(swfMajorVersion, swfMinorVersion, swfRevision);
    if (hasRequestedVersion)
    {
        AC_FL_RunContent(
        "src", swfPath,
        "width", swfWidth,
        "height", swfHeight,
        "align", "middle",
        "id", ("teaserFlash"+flashInstance++),
        "quality", "high",
        "bgcolor", swfBgColor,
        "name", ("teaserFlash"+flashInstance++),
        "allowScriptAccess","sameDomain",
        "type", "application/x-shockwave-flash",
        'codebase', 'http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab',
        "pluginspage", "http://www.adobe.com/go/getflashplayer",
        'FlashVars', 'linkurl='+linkPath
        );
    }
    else
    {
        var alternateContent = '<a href="' + linkPath + '"><img src="' + imgPath + '" alt="' + imgAlt + '" width="' + imgWidth + '" height="' + imgHeight + '" /></a>';
        document.write(alternateContent);
    }
}

So, now we need a rendering which writes a script tag calling this helper function, and a noscript tag which writes the flat image for users without Javascript. I had to work out how to access specific fields of the Flash and image objects in order to pass the raw values into the Javascript function.

The full rendering code, which iterates through all the selected teasers on the home page, is as follows:

<?xml version="1.0" encoding="UTF-8"?>

<!--=============================================================
    File: Home Page Teasers.xslt
    Created by: sitecore\mike
    Created: 02/07/2008 16:48:43
    Copyright notice at bottom of file
==============================================================-->

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:sc="http://www.sitecore.net/sc"
  xmlns:dot="http://www.sitecore.net/dot"
  exclude-result-prefixes="dot sc">

<!-- output directives -->
<xsl:output method="html" indent="no" encoding="UTF-8" />

<!-- parameters -->
<xsl:param name="lang" select="'en'"/>
<xsl:param name="id" select="''"/>
<xsl:param name="sc_item"/>
<xsl:param name="sc_currentitem"/>

<!-- variables -->
<xsl:variable name="home" select="/*/item&#91;@key='content'&#93;/item&#91;@key='home'&#93;" />

<!-- entry point -->
<xsl:template match="*">
  <xsl:apply-templates select="$sc_item" mode="main"/>
</xsl:template>

<!--==============================================================-->
<!-- main                                                         -->
<!--==============================================================-->

<xsl:template match="*" mode="main">
    <xsl:variable name="ids" select="concat(sc:fld('teasers',.),'|')"/>
    <xsl:if test="string-length($ids)>1">
<ul class="teasers">
            <xsl:call-template name="doTeasers">
                <xsl:with-param name="ids" select="$ids"/>
            </xsl:call-template></ul>
</xsl:if>
</xsl:template>

<xsl:template name="doTeasers">
    <xsl:param name="ids"/>
    <xsl:if test="$ids">
        <xsl:variable name="itm_id" select="substring-before($ids, '|')"/>
        <xsl:variable name="itm_rest" select="substring-after($ids, '|')"/>
        <xsl:variable name="is_more" select="boolean(string-length($itm_rest)>0)"/>

        <xsl:if test="$itm_id">
            <xsl:variable name="itm" select="sc:item($itm_id,.)"/>
            <xsl:if test="sc:fld('flash',$itm)=''">
	<li>
                    <xsl:if test="not($is_more)">
                        <xsl:attribute name="class">last</xsl:attribute>
                    </xsl:if>
                    <sc:link select="sc:item(sc:fld('link',$itm,'id'),.)">
                        <sc:image field="image" select="$itm" />
                    </sc:link></li>
</xsl:if>
            <xsl:if test="sc:fld('flash',$itm)!=''">
	<li>
                    <xsl:if test="not($is_more)">
                        <xsl:attribute name="class">last</xsl:attribute>
                    </xsl:if>
                    <script type="text/javascript">
                        doTeaser("<xsl:value-of select="concat('/',sc:GetMediaUrl(sc:item(sc:fld('flash',$itm),.)))" />", <sc:text field="width" select="sc:item(sc:fld('flash',$itm),.)" />, <sc:text field="height" select="sc:item(sc:fld('flash',$itm),.)" />, <sc:text field="version" select="sc:item(sc:fld('flash',$itm),.)" />, 0, 0, "", "<xsl:value-of select="concat('/',sc:fld('image',$itm,'src'))" />", <sc:text field="width" select="sc:item(sc:fld('image',$itm,'mediaid'),.)" />, <sc:text field="height" select="sc:item(sc:fld('image',$itm,'mediaid'),.)" />, "<sc:text field="alt" select="sc:item(sc:fld('image',$itm,'mediaid'),.)" />", "<xsl:value-of select="sc:path(sc:item(sc:fld('link',$itm,'id'),.))" />");
                    </script>
                    <noscript>
                        <sc:link select="sc:item(sc:fld('link',$itm,'id'),.)">
                            <sc:image field="image" select="$itm" />
                        </sc:link>
                    </noscript></li>
</xsl:if>

            <xsl:if test="$is_more">
                <xsl:call-template name="doTeasers">
                    <xsl:with-param name="ids" select="$itm_rest"/>
                </xsl:call-template>
            </xsl:if>

        </xsl:if>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

This outputs the teasers inside an unordered list, which I’ve styled up using CSS.

Finally, here’s an example showing how to hook up the “linkurl” FlashVars parameter in the Flash movie. On the first frame of the movie, set _global.linkurl like this:

Accessing the FlashVars parameter from within Flash

Then, create a button with a hit area the same size as the teaser, and in its onrelease function, call getURL(linkurl) like this:

Linking the button to the URL

Hope all that makes sense!

From → flash, sitecore

6 Comments
  1. Hi there,

    We have tried this code out but are getting errors. We are using Sitecore 5.3. Do you know if this will work with that version? I’ve had to substitute the Droplink field with a lookup field which is the only difference from your instructions.

    Thanks.

    Regards,

    Joanne Innes

  2. Mike permalink

    Hi Joanne

    At what point are you getting the errors, and what are they exactly?

    Thanks,

    Mike

  3. We have stripped back the code to try to identify the problem.

    We have traced it down to 2 problems:

    1. On the xslt file the line that calls the doTeaser function is creating the error.
    So we tried calling this function without any parameters (no error) and of course changed the actual doTeaser function so that the parameters are not being looked for and hardwired the actual values into the AC_FL_RunContent function.

    This is where the 2nd problem is occurring. Everything runs without an error but right clicking the flash movie object says that no movie is loaded.

    So the flash object is installed and is displaying on the page correctly without errors. It looks like a pathing issue on the src parameter. We have tested this url by simply typing it into IE and it works.

    Any advice is greatly appreciated.

    Tried to copy the function in but got a discarded message.

    Thanks.

    Joanne

  4. Mike permalink

    Hi Joanne

    How have you hard-coded the parameters? On my v6, the XSL produces this Javascript:

    doTeaser("/~/media/Flash/Home page teasers/test.ashx", 290, 150, 8, 0, 0, "", "/~/media/Images/Home page teasers/test.ashx", 290, 150, "Home page teaser test image", "/en/our-work.aspx");
    

    Do these parameters match the kind of thing you’ve hard coded into your AC_FL_RunContent function? (In particular, do the paths to the .ashx files start with a forward slash? eg ‘/~/…’)

    Mike

  5. We harcoded the following in the AC_FL_RunContent to try to get a swf file to be viewable.

    AC_FL_RunContent(
    ‘codebase’, ‘http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0’,
    ‘width’, ‘550’,
    ‘height’, ‘400’,
    ‘src’, ‘http://quarriersdev.org.uk/layouts/example’,
    ‘quality’, ‘high’,
    ‘pluginspage’, ‘http://www.macromedia.com/go/getflashplayer’,
    ‘align’, ‘middle’,
    ‘play’, ‘true’,
    ‘loop’, ‘true’,
    ‘scale’, ‘showall’,
    ‘wmode’, ‘window’,
    ‘devicefont’, ‘false’,
    ‘id’, ‘example’,
    ‘bgcolor’, ‘#ffffff’,
    ‘name’, ‘example’,
    ‘menu’, ‘true’,
    ‘allowScriptAccess’,’sameDomain’,
    ‘movie’, ‘example’,
    ‘salign’, ”
    );

    Thanks.

    Joanne

  6. Mike permalink

    Hi Joanne

    In my instructions, I modify the AC_FL_RunContent function so that it stops hard-coding the .swf extension. Have you tried adding “.swf” onto your ‘src’ parameter?

    eg:

    ’src’, ‘http://quarriersdev.org.uk/layouts/example.swf’,

    Cheers,

    Mike

Leave a comment