Adding Sound

What did you say? I can't hear you! Maybe you should add some MP3s to your Ming generated swf files!

At this time, MP3s are the only sound format available to us in Ming, and only fully upgraded WebTv units seem to support them. There are workarounds for using other sound files outside of your Ming swf file using action scripting and javascript, but that's another lesson!

Today, let's add a MP3 wolf sound effect to a moving moon shape (you can of course make better looking moons, this is basic for example).

A) Begin as always by starting your html document and opening your php code block

<html>
<body>
<?php

$moonGradient=new SWFGradient();
$moonGradient->addEntry(0.0,225,225,225);
$moonGradient->addEntry(0.25,200,200,200);
$moonGradient->addEntry(0.5,175,175,175);
$moonGradient->addEntry(0.75,125,125,125);
$moonGradient->addEntry(1.0,0,0,0);

C) Next, let's create our moon shape using our circle code from previous lessons and the gradient fill we just created

$myMoon=new SWFShape();
$moonFill=$myMoon->addFill($moonGradient, SWFFILL_RADIAL_GRADIENT);
$moonFill->scaleTo(0.1);
$moonFill->moveTo(-20,20);
$myMoon->setRightFill($moonFill);
$ra = 50;
$a = $ra * 0.414213562; // = tan(22.5 deg)
$b = $ra * 0.707106781; // = sqrt(2)/2 = sin(45 deg)
$myMoon->movePenTo($x+$ra, $y);
$myMoon->drawCurveTo($x+$ra,$y-$a,$x+$b,$y-$b);
$myMoon->drawCurveTo($x+$a,$y-$ra,$x,$y-$ra);
$myMoon->drawCurveTo($x-$a,$y-$ra,$x-$b,$y-$b);
$myMoon->drawCurveTo($x-$ra,$y-$a,$x-$ra,$y);
$myMoon->drawCurveTo($x-$ra,$y+$a,$x-$b,$y+$b);
$myMoon->drawCurveTo($x-$a,$y+$ra,$x,$y+$ra);
$myMoon->drawCurveTo($x+$a,$y+$ra,$x+$b,$y+$b);
$myMoon->drawCurveTo($x+$ra,$y+$a,$x+$ra,$y);

D) Now, we define our movie and set it's attributes as always

$myMovie=new SWFMovie();
$myMovie->setDimension(300,200);
$myMovie->setBackground(0,0,0);
$myMovie->setRate(40);

E) Next, we add our moon shape and position it, nothing new here

$moonShape=$myMovie->add($myMoon);
$moonShape->moveTo(-50,250);

F) Now, let's add our wolf MP3 sound effect, notice that we use the standard php fopen command with a "r" attribute to read the file

$myMovie->streamMp3(fopen("DarkShadows.mp3", "r"));

G) Next, we animate our moon shape, using a loop though you can animate it step by step if loops make you uncomfortable

for($i=0; $i<400; $i++){
$moonShape->moveTo(($i-50),(250-$i*0.75));
$myMovie->nextFrame();
}

H) When adding MP3s to your Ming generated swf files, you should specify the number of frames needed, which you get by multiplying the number of seconds of your MP3 by the rate you set your movie to. This wolf MP3 is 10 seconds and the rate is set to 40 so our total frames equals 400

$myMovie->setFrames(400);

G) Finally, we save our movie, close our php code block, and add our html object tags as always

$myMovie->save("lesson14.swf");
?>
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0" ID=objects WIDTH=300 HEIGHT=200>
<PARAM NAME=movie VALUE="lesson14.swf">
<EMBED src="lesson14.swf" WIDTH=300 HEIGHT=200 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">
</EMBED>
</OBJECT>
</BODY>
</html>


What? I couldn't hear you!

Code summary for lesson 14


Result: