How can I play a sound in a specified intervals?

Posted on 2009-12-28
Last Modified: 2013-11-11
I am working on a metronome in as3.  I would like to be able to have the user specify what interval they would like to hear the metronome tick.     So My idea is to start a counter when the swf loads and play the tick sound when ever the specified time elapsed on the counter.  So if the interval time was 2 seconds I would like to play the metronome tick every 2 seconds.  Here Is the code I have so far.

The condition in the if statement is what I need help with .  "x amount of seconds has progressed"  What can I use to check to see if a certain amount of time has passed to play the sound?  
var startTime:Number= 0;

stage.addEventListener(MouseEvent.CLICK, reset);

function reset(myevent:MouseEvent):void {

	startTime = getTimer();


stage.addEventListener(Event.ENTER_FRAME, showTime);

function showTime(myevent:Event):void {

	var currentTime:Number = getTimer();

	var elapsedTime:Number = (currentTime - startTime) / 1000;

	myDisplay_txt.text = elapsedTime.toString();

	if ( x amount of seconds has progressed){

		play sound();






Open in new window

Question by:ForLoop5
    LVL 19

    Accepted Solution

    change startTime to:

    var startTime:Number=getTimer();

    then elapsedTime will be the number of seconds that have elapsed.

    so you could say

    if(elapsedTime > 2)

    means more than two seconds have elapsed since the app was loaded.

    that said, it'd be a lot easier to just use a timer
    function showTime(myevent:Event):void {
      play sound();
    var timer:Timer = new Timer(2000); // set a timer to "tick" every 2 seconds
    timer.addEventListener("timer", showTime); // add the handler

    Open in new window

    LVL 1

    Author Comment

    Nice.  How can I set the "2000" to be a variable from an input text field?

    var timer:Timer = new Timer(inputText);

    LVL 1

    Author Closing Comment

    Thank you.  I figured out the second part.  Heres how I did it

    var myNumber:Number = Number(beat.text);
    var myTimer:Timer=new Timer(myNumber);
    function movecar(myevent:TimerEvent):void {

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
    The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
    The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
    The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now