update:
pause() method is added, example .fla file is updated.
As far as I know there is no way for adding cue points in flash Sound, but using SoundChannel.position property we can check playhead current position and simulate cue points for Sound.
So I wrote simple utility Class for simulating cue points for Sound Class In AS3.
Example:
AudioCuePoint.as
Actionscript:
-
package com.abrahamyan.util
-
{
-
-
/**
-
* ...
-
* @author Armen Abrahamyan
-
*/
-
-
import flash.events.Event;
-
import flash.media.Sound;
-
import flash.media.SoundChannel;
-
import flash.utils.Timer;
-
import flash.events.TimerEvent;
-
import flash.events.EventDispatcher;
-
public class AudioCuePoint extends EventDispatcher
-
{
-
public static const ON_CUEPOINT:String = "on_cuepoint";
-
private var _soundChannel:SoundChannel;
-
private var _audioTimer:Timer;
-
private var _timerInterval:Number = .1;
-
private var _usedCuePoints:Array;
-
private var _cuepoints:XMLList;
-
private var _cuepointTime:Number;
-
private var _cuepointText:String;
-
private var _accuracy:Number = .1;
-
private var _paused:Boolean = false;
-
public var destroyAfterComplete:Boolean = false;
-
/**
-
*
-
* @param pSoundChannel
-
* @param pCuepoints
-
*/
-
public function AudioCuePoint(pSoundChannel:SoundChannel=null,pCuepoints:XMLList =null):void
-
{
-
_soundChannel = pSoundChannel;
-
_cuepoints = pCuepoints;
-
_usedCuePoints = [];
-
_usedCuePoints.length = _cuepoints.length();
-
}
-
-
public function set soundChannel(pSoundChannel:SoundChannel):void
-
{
-
_soundChannel = pSoundChannel;
-
}
-
public function get soundChannel():SoundChannel
-
{
-
return _soundChannel;
-
}
-
public function set cuepoints(pCuepoints:XMLList):void
-
{
-
_cuepoints = pCuepoints;
-
_usedCuePoints.length = _cuepoints.length();
-
}
-
public function get cuepoints():XMLList
-
{
-
return _cuepoints;
-
}
-
public function get cuepointTime():Number
-
{
-
return _cuepointTime;
-
}
-
public function get cuepointText():String
-
{
-
return _cuepointText;
-
}
-
/**
-
* starts process of finding cuepoints
-
*/
-
public function start():void
-
{
-
if (!_soundChannel || !_cuepoints)
-
{
-
return;
-
-
}
-
if (_paused)
-
{
-
_paused = false;
-
_audioTimer.start();
-
return;
-
}
-
-
_audioTimer = new Timer(_timerInterval * 1000);
-
_audioTimer.addEventListener(TimerEvent.TIMER, onAudioTimerHandler);
-
_audioTimer.start();
-
-
if (destroyAfterComplete)
-
{
-
_soundChannel.addEventListener(Event.SOUND_COMPLETE,onSoundComplete,false,0,true)
-
}
-
-
-
-
}
-
/**
-
* pause
-
*/
-
public function pause():void
-
{
-
if (_paused)
-
{
-
return;
-
}
-
_paused = true;
-
_audioTimer.stop();
-
}
-
public function get paused():Boolean
-
{
-
return _paused;
-
}
-
/**
-
* when sound complete
-
* @param e
-
*/
-
private function onSoundComplete(e:Event):void
-
{
-
_soundChannel.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete);
-
destroy();
-
}
-
/**
-
*
-
* @param e
-
*/
-
private function onAudioTimerHandler(e:TimerEvent):void
-
{
-
trace("tick");
-
var position:Number = Math.round(_soundChannel.position / 100)/10;
-
checkForCuePoints(position);
-
}
-
/**
-
* checks time with cuepoints list
-
* @param pTime
-
*/
-
private function checkForCuePoints(pTime:Number):void
-
{
-
var cnt:Number = cuepoints.length();
-
for (var i:uint = 1; i <= cnt; i++)
-
{
-
if (pTime>= (Number(cuepoints[i - 1].@time) - _accuracy) && pTime <= (Number(cuepoints[i - 1].@time) + _accuracy) && (!_usedCuePoints[i - 1] ))
-
{
-
_usedCuePoints[i - 1] = true;
-
_cuepointTime = pTime;
-
_cuepointText = cuepoints[i - 1];
-
dispatchEvent(new Event(ON_CUEPOINT));
-
break;
-
}
-
-
-
}
-
}
-
/**
-
* removes listeners/loops/onenterframes...
-
*/
-
public function destroy():void
-
{
-
if (_audioTimer)
-
{
-
_audioTimer.stop();
-
_audioTimer.removeEventListener(TimerEvent.TIMER, onAudioTimerHandler);
-
}
-
if (destroyAfterComplete)
-
{
-
_soundChannel.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete);
-
}
-
}
-
-
-
}
-
-
}
nice Tutorial! Thanks
I would like to use this class to control the fade in/out of movieclips on stage. How would you extend/change the XML and the onCuepointFind function to fire something like
fadeOut = new Tween(mc, “alpha”,None.easeNone,1,0,2,true);
Any hint appreciated.
I solved it like this;
XML:
var cuepoints:XML =
cue point 1
cue point 2
cue point 2
added 2 new properties _targetObj and _tween to the Class and added the values to be dispatched with the checkForCuePoints() function.
in the FLA I changed:
function onCuepointFind(e:Event):void
{
var _time = AudioCuePoint(e.target).cuepointTime;
var _text = AudioCuePoint(e.target).cuepointText;
var _targetClip = AudioCuePoint(e.target).cuepointTarget;
var _tween = AudioCuePoint(e.target).cuepointTween;
output_txt.appendText(”cuepoint find time: ” +_time+ ” / text: “+_text+”/”+_targetClip+”–”+_tween+”\n”);
output_txt.scrollV = output_txt.maxScrollV
this[_tween](this[_targetClip]); //this was hardest!!
}
and added the methods fadedIn and fadeOut
function fadeOut(mc:MovieClip):void
{
TweenLite.to(mc, 1, {alpha:0});
}
function fadeIn(mc:MovieClip):void
{
TweenLite.to(mc, 1, {alpha:1});
}
Works great, thanks!
@lunacafu.
Well, if u are tweening something that out of Class you don’t have to add properties inside class.
you catch the event onCuepointFind, u got the time and text, your object/movieclip that u wanna tween is out of class, just have some fucntion to show it(outside of class), and call that method inside onCuepointFind.
Its almost the same thing you did, but not inside Class file itself.
@lunacafu.
do you have an example of what you did online
I would love to see it in action
thanks both of you for the info.
I am sure this will come in handy.
Not sure yet, but this may be EXACTLY what I need!!! Thanks in advance!
This is indeed a very helpful class!
Is it possible to add pause functionality? Meaning that when I pause my sound and then resume it, the cue point timer will also pick up where it left.
Thanks, this is a keeper.
@Lasse thx for pointing.
puase() method is added, for the resume use the same start().
When resume, make sure to set the soundChannel property again:
audioCuePoint.start();
sndChannel= snd.play(sndChannel.position);
audioCuePoint.soundChannel = sndChannel;
source files are updated.