A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [F8] Specifying more accurate cuepoints?

  1. #1
    Junior Member
    Join Date
    Mar 2007
    Posts
    8

    [F8] Specifying more accurate cuepoints?

    Hey all,
    Im trying to call a function at certains beats in an mp3. To do this I want to use cue points and to type them manually for control. However, I cant get the timing more accurate than in seconds using actionscript, it doesnt seem to understand sat 2.5 as opposed to 2. Maybe the syntax is wrong? This is the code I am using to do it.


    var aCuePoints:Array = [{cue:3, msg:"Cue point 1"},
    {cue:5, msg:"Cue point 2"},
    {cue:7, msg:"Cue point 3"},
    {cue:9, msg:"Cue point 4"}
    ]

    var sndMusic:Sound = new Sound();
    sndMusic.loadSound("Neverending.mp3", true);

    var nCuePointCheck:Number = setInterval(checkCue, 1000);

    function checkCue():Void{
    for(var i:Number=0; i<aCuePoints.length; i++){
    if(aCuePoints[i].cue == Math.floor(sndMusic.position/1000)){
    trace(aCuePoints[i].msg);
    break;
    }

    }
    }

    As you can see the cue points are at 3, 5, 7 seconds etc but i need them inbetween.

    Any help would be MUCH appreciated!
    I dont know any XML either and embedding them into the movie makes them difficult to alter.

  2. #2
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    set a faster interval time and round the sound.position
    to one decimal place. this example loads from an xml file
    Code:
    var aCuePoints:Array = new Array();
    var sndMusic:Sound = new Sound();
    var nCuePointCheck:Number;
    
    var _xml:XML = new XML();
    _xml.ignoreWhite = true;
    _xml.load("cue.xml");
    
    /*--cue.xml--
    <?xml version="1.0" encoding="UTF-8"?>
    <list>
    <cues cue="3.2" msg="Cue point 1" />
    <cues cue="5.7" msg="Cue point 2" />
    <cues cue="7.4" msg="Cue point 3" />
    <cues cue="9.5" msg="Cue point 4" />
    </list>
    */
    
    _xml.onLoad = function():Void{
    aNode = this.firstChild.childNodes;
    len = aNode.length;
    for(var n:Number=0;n!=len;n++){
    var obj:Object = new Object();
    obj.cue = aNode[n].attributes.cue;
    obj.msg = aNode[n].attributes.msg;
    aCuePoints.push(obj);
    }
    checker();
    };
    
    function checker():Void{
    sndMusic.loadSound("evelyn.mp3", true);
    nCuePointCheck = setInterval(checkCue, 25);
    };
    
    function checkCue():Void{
    var val:Number = int((sndMusic.position/1000)*10)/10;
    trace(val); 
    for(var i:Number=0; i<aCuePoints.length; i++){
    if(aCuePoints[i].cue == val){
    trace(aCuePoints[i].msg);
    break;
    } 
    } 
    };
    hth

  3. #3
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    a_modified_dog got there before me, this is an alternative using onEnterFrame instead and also altering your cues to milliseconds rather than seconds.
    code:

    var aCuePoints:Array;
    var s:Sound = new Sound();
    s.loadSound("E:\\21NewMedia\\mp3s\\mortgage_payoff Mycash9.mp3", false);
    var xm = new XML();
    xm.onLoad = function(success:Boolean) {
    if (success) {
    aCuePoints = new Array();
    var cues = this.firstChild.childNodes;
    for (var i = 0; i<cues.length; i++) {
    var o:Object = new Object();
    var cuenodes = cues[i].childNodes;
    trace(cuenodes.length);
    for (var j = 0; j<cuenodes.length; j++) {
    if (cuenodes[j].nodeName == "cue") {
    o[cuenodes[j].nodeName] = Number(cuenodes[j].firstChild.nodeValue);
    } else {
    o[cuenodes[j].nodeName] = cuenodes[j].firstChild.nodeValue;
    }
    }
    aCuePoints.push(o);
    s.start();
    onEnterFrame = checkCues;
    }
    }
    };
    xm.ignoreWhite = true;
    xm.load("cues.xml");
    function checkCues() {
    var p = s.position;
    // Check from the top backwards so as to ensure most current cue
    var currentCue:String = "";
    for (var i = 0; i<aCuePoints.length; i++) {
    if (aCuePoints[i].cue>p) {
    break;
    }
    currentCue = aCuePoints[i].msg;
    }
    trace("p is "+p+" and i is "+i+" CURRENT CUE IS : "+currentCue);
    }


    ps: the XML for this is:
    code:

    <cues>
    <cuepoint>
    <cue>3000</cue>
    <msg>Cue point 1</msg>
    </cuepoint>
    <cuepoint>
    <cue>5000</cue>
    <msg>Cue point 2</msg>
    </cuepoint>
    <cuepoint>
    <cue>7000</cue>
    <msg>Cue point 3</msg>
    </cuepoint>
    <cuepoint>
    <cue>9000</cue>
    <msg>Cue point 4</msg>
    </cuepoint>
    </cues>


  4. #4
    Junior Member
    Join Date
    Mar 2007
    Posts
    8
    Great, thanks guys! Ill try those out and let you know how I get on!
    But AlsoGaiusCoffey, where are the milliseconds? The numbers in the xml?

  5. #5
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    Yes, each cuepoint in the XML has a cue node and a msg node. Set the cue node nodeValue to whatever milliseconds you want.

  6. #6
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    Hi Laver2k, in response to your message, to call a function when the message changes, you need to know what the old cue message is.
    One way would be to alter the checkCues() function, eg:
    code:

    var oldCue:String = "";
    function checkCues() {
    var p = s.position;
    // Check from the top backwards so as to ensure most current cue
    var currentCue:String = "";
    for (var i = 0; i<aCuePoints.length; i++) {
    if (aCuePoints[i].cue>p) {
    break;
    }
    currentCue = aCuePoints[i].msg;
    }
    if(oldCue != currentCue) {
    trace("CUE HAS CHANGED FROM "+oldCue+" TO "+currentCue);
    oldCue = currentCue;
    // Call your function now as you know the cue has changed
    nextBall(currentCue);
    }
    trace("p is "+p+" and i is "+i+" CURRENT CUE IS : "+currentCue);
    }


  7. #7
    Junior Member
    Join Date
    Mar 2007
    Posts
    8
    great! thanks so much everybody!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center