
Код AS3:
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.net.URLRequest;
public class ShortTimer extends MovieClip
{
public var snd:Sound;
public var chnl:SoundChannel;
public var transf:SoundTransform;
public var position:uint;
public var timerVol:Timer = new Timer(10);
protected var volume:Number = 1;
public var flag:Boolean;
public function ShortTimer()
{
flag = true;
sndBtn.buttonMode = true;
snd = new Sound(new URLRequest("simple.mp3"));
chnl = snd.play(0, 10000);
transf = new SoundTransform( 1 );
sndBtn.addEventListener(MouseEvent.CLICK, onSndBtnClicked);
timerVol.addEventListener(TimerEvent.TIMER, onTimerListener);
trace(transf.volume);
}
//кнопка
public function onSndBtnClicked(event:MouseEvent):void
{
timerVol.start();
sndBtn.play();// у sndBtn два кадра: "выкл." и "вкл."
//chnl.soundTransform = transf;
flag = !flag;
}
//таймер
public function onTimerListener(event:TimerEvent):void
{
if (flag)
{
volume += .01;
} else
{
volume -= .01;
}
//не выходить из пределов от 0 до 1
if (volume > 1)
{
volume = 1;
timerVol.reset();
}
if ( volume < 0 )
{
volume = 0;
timerVol.reset();
}
setVolume ();
trace("transf.volume = " + transf.volume);
}
private function setVolume ()
{
transf.volume = volume;
chnl.soundTransform = transf;
}
}
}