13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with sound<br />

import flash.ev<strong>en</strong>ts.Ev<strong>en</strong>t;<br />

import flash.media.Sound;<br />

import flash.media.SoundChannel;<br />

import flash.media.SoundMixer;<br />

import flash.net.URLRequest;<br />

var snd:Sound = new Sound();<br />

var req:URLRequest = new URLRequest("bigSound.mp3");<br />

snd.load(req);<br />

var panCounter:Number = 0;<br />

var trans:SoundTransform;<br />

trans = new SoundTransform(1, 0);<br />

var channel:SoundChannel = snd.play(0, 1, trans);<br />

channel.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.SOUND_COMPLETE, onPlaybackComplete);<br />

addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.ENTER_FRAME, onEnterFrame);<br />

function onEnterFrame(ev<strong>en</strong>t:Ev<strong>en</strong>t):void<br />

{<br />

trans.pan = Math.sin(panCounter);<br />

channel.soundTransform = trans; // or SoundMixer.soundTransform = trans;<br />

panCounter += 0.05;<br />

}<br />

function onPlaybackComplete(ev<strong>en</strong>t:Ev<strong>en</strong>t):void<br />

{<br />

removeEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.ENTER_FRAME, onEnterFrame);<br />

}<br />

This code starts by loading a sound file and th<strong>en</strong> creating a new SoundTransform object with volume set to 1 (full<br />

volume) and pan set to 0 (ev<strong>en</strong>ly balanced betwe<strong>en</strong> left and right). Th<strong>en</strong> it calls the snd.play() method, passing the<br />

SoundTransform object as a parameter.<br />

While the sound plays, the onEnterFrame() method executes repeatedly. The onEnterFrame() method uses the<br />

Math.sin() function to g<strong>en</strong>erate a value betwe<strong>en</strong> -1 and 1, a range that corresponds to the acceptable values of the<br />

SoundTransform.pan property. The SoundTransform object’s pan property is set to the new value, and th<strong>en</strong> the<br />

channel’s soundTransform property is set to use the altered SoundTransform object.<br />

To run this example, replace the fil<strong>en</strong>ame bigSound.mp3 with the name of a local mp3 file. Th<strong>en</strong> run the example. You<br />

should hear the left channel volume getting louder while the right channel volume gets softer, and vice versa.<br />

In this example, the same effect could be achieved by setting the soundTransform property of the SoundMixer class.<br />

However, that would affect the panning of all sounds curr<strong>en</strong>tly playing, not just the single sound being played by this<br />

SoundChannel object.<br />

Working with sound metadata<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

Sound files that use the mp3 format can contain additional data about the sound in the form of ID3 tags.<br />

Last updated 6/6/2012<br />

456

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!