spacer
Yehuda Shiran May 23, 2001
Embedding Flash
Tips: May 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

In some of your applications you may need to use Flash Audio and JavaScript directly, without any prepackaged APIs such as FlashSound JavaScript API. You will enjoy more features, methods, and properties, but you'll have to take care of all the tiny details that are taken for granted when using FlashSound API.

One of the "tiny" details is embedding the sound object. The JavaScript file included in this tip, flashsoundcheck.js, does exactly this, and other stuff. It also checks for Flash plug-ins, and minimum player version:

winIEpass = ((navigator.appName.indexOf("Microsoft") != -1) && 
(navigator.appVersion.indexOf("Windows") != -1)) && 
(parseFloat(navigator.appVersion) >= 4) ? true : false;

NNpass = ((navigator.appName == "Netscape") && 
(navigator.userAgent.indexOf("Mozilla") != -1) && 
(parseFloat(navigator.appVersion) >= 4) && 
(navigator.javaEnabled())) ? true : false;

supportedBrowser = (winIEpass || NNpass) ? true : false;

// check for Flash Plug-in in Mac or Win Navigator. Get plug-in version.


minPlayer = 4;
var mySwf;

function Flash_checkForPlugIn()
{
var plugin = (navigator.mimeTypes &&
navigator.mimeTypes["application/x-shockwave-flash"]) ?
navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
if (plugin) {
var pluginversion = parseInt(plugin.description.substring(plugin.description.indexOf(".")-1)) 
if(pluginversion >= minPlayer) {return true;}
}
return false;
}

// vbscript check for Flash ActiveX control in windows IE
if(supportedBrowser && winIEpass)
{
document.write(
		'<script language=VBScript>' + '\n' +
		'Function Flash_checkForActiveX()' + '\n' +
			'Dim hasPlayer, playerversion' + '\n' +
			'hasPlayer = false' + '\n' +
			'playerversion = 10' + '\n' +
			'Do While playerversion >= minPlayer' + '\n' +
				'On Error Resume Next' + '\n' +
				'hasPlayer = (IsObject(CreateObject(\"ShockwaveFlash.ShockwaveFlash.\" & playerversion & \"\")))' + '\n' +
				'If hasPlayer = true Then Exit Do' + '\n' +
				'playerversion = playerversion - 1' + '\n' +
			'Loop' + '\n' +
			'Flash_checkForActiveX = hasPlayer' + '\n' +
		'End Function' + '\n' +
		'<\/script>'
		);
}



function Flash_checkForMinPlayer()
{
if(!supportedBrowser) return false;
if(NNpass) return (Flash_checkForPlugIn());
if(winIEpass) return (Flash_checkForActiveX());
}

function Flash_embedSWF(srcURL, swfbgColor)
{

if (!Flash_checkForMinPlayer()) return;

var defaultColor = (document.bgColor != null) ? document.bgColor : "#ffffff";
var bgcolor = (swfbgColor != null) ? swfbgColor : defaultColor;

document.writeln(
'<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' +
'codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0"' +
'ID="sonify" WIDTH=1HEIGHT=1>' +
'<PARAM NAME=movieVALUE="' + srcURL + '">' + 
'<PARAM NAME=qualityVALUE=low>' +
'<PARAM NAME=wmodeVALUE=transparent>'+
'<PARAM NAME=bgcolorVALUE=' + bgcolor + '>' +
'<EMBED swLiveConnect="true" NAME="sonify"' +
'src="' + srcURL + '"' +
'quality=low' +
'wmode=transparent' +
'bgcolor=' + bgcolor + 
'WIDTH=1 HEIGHT=2' +
'TYPE="application/x-shockwave-flash"'+	
'PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">' +
'</EMBED>' + 
'</OBJECT>'
);
}

function sonified_flash(myFrame){
if(!Flash_checkForMinPlayer()) {return;}

	mySwf = window.document.sonify;
	if (mySwf.PercentLoaded()<100) return

	mySwf.GotoFrame(myFrame);
	mySwf.GotoFrame(0);
}

To be able to run JavaScript with embedded sound, you need to do two things. First, specify your sound track. You do it with the Flash_embedSWF() function:

Flash_embedSWF("animalsounds.swf");
Secondly, you need to create a Flash object. Since we named our embedded object sonify, you always define your objects as window.document.sonify:

mySwf = window.document.sonify;
Once an object exists, we can test all Flash methods and properties. The first link below demonstrates the Play() method. You call mySwf.Play(). Click it and listen to the sound track of the barking dog. Notice it takes a few seconds before the barking dog is heard. The reason is that the barking dog start at frame 10, and the Play() method starts at the beginning of the track, at frame 0:

| Play | Dog Barking | Frog Ribbet | Horse Whinny | Cat meow |

Here is the source code:

|
<A href="javascript://" onclick="javascript:mySwf.Play(); return false">Play</a> |
<A href="javascript://" onclick="sonified_flash(10); return false">Dob Barking</a> |
<A href="javascript://" onclick="sonified_flash(30); return false">Frog Ribbet</a> |
<A href="javascript://" onclick="sonified_flash(60); return false">Horse Whinny</a> |
<A href="javascript://" onclick="sonified_flash(80); return false">Cat meow</a> |


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business