Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8468409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:55:25+00:00 2026-06-10T15:55:25+00:00

I have this Timer that is supposed to initiate various actions at 1 second

  • 0

I have this Timer that is supposed to initiate various actions at 1 second intervals. It’s a pretty simple idea that simulates a 5 second countdown (literally). At the start, a JLabel is updated to set its text to “5”. Simultaneously, a little mp3 sound file plays that voices the number that the user sees on the screen. Then, one second later, the text is changed to “4” and a different mp3 plays that voices the number 4. And so on until we get to zero.

This all works, but I can’t get the visual updating to synchronize precisely with the audio part. The mp3 always seems to play just slightly before the screen is updated. At first, I thought that I just needed to put a little extra silence at the beginning of each mp3 on a trial and error basis until things synched up. But no matter how much silence I prepend to each mp3, I still hear the audio before the screen updates. All that changes is the lag between each “one second” update.

Anyway, here is the code that I am working with. Can anyone help me get this to synchronize? Maybe I need a second timer? I’m not sure how that would work. Thanks in advance!

class Countdown extends JFrame implements ActionListener {

    private Timer countdownTimer = new Timer(1000, this);
    int countdownSeconds;
    MyJFrame myFrame;

    public Countdown(MyJFrame thisFrame) {

        int countdownSeconds = 5;
        countdownTimer.start();
        myFrame = thisFrame;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

            if (countdownSeconds == 0) {
                myFrame.updateCountdown(myFrame, "Go");
                SoundEffect.play("launch.mp3");
                countdownTimer.stop();
            } else {
                myFrame.updateCountdown(myFrame, Integer.toString(countdownSeconds));
                if (countdownSeconds == 5) {SoundEffect.play("five.mp3");}
                if (countdownSeconds == 4) {SoundEffect.play("four.mp3");}
                if (countdownSeconds == 3) {SoundEffect.play("three.mp3");}
                if (countdownSeconds == 2) {SoundEffect.play("two.mp3");}
                if (countdownSeconds == 1) {SoundEffect.play("one.mp3");}
                countdownSeconds--;
            }
        }
    }

public void updateCountdown(MyJFrame thisFrame, String numSec) {
    lblCountdown.setText(numSec);
}



import java.io.FileInputStream;
import javazoom.jl.player.Player;

public class SoundEffect {

    public static void play(String mp3File) {
        try {
            FileInputStream mp3_file = new FileInputStream(mp3File);
            Player mp3 = new Player(mp3_file);
            mp3.play();
        }
        catch(Exception e) {
            System.out.println(e);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:55:27+00:00Added an answer on June 10, 2026 at 3:55 pm

    I highly doubt you will ever be able to sync those perfectly, but I can explain why the current approach will not work.

    Swing components must be updated on the Event Dispatch Thread, as you do with the Timer. When you update the text of the label, it will schedule a repaint on the Event Dispatch Thread. Note the word schedule, and not perform.

    However, the Event Dispatch Thread is currently busy playing your sound, so the actual repaint operation will only occur after you called mp3.play().

    Now you could (if allowed, not sure about the threading rules for playing MP3’s) try to play the mp3 on a different Thread (e.g. by using a secondary non-Swing timer). But since you never have full control over when the actual repaint is going to happen and only can control when the repaint is scheduled, the visual and auditive updates can still be out-of-sync.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple form that is supposed to start a timer, perform a
i have a question , i have this timer code in java that when
i need a timer that works like this : imagine somebody have a limit
I have this div that is supposed to be hidden when the page loads
I am aware that System.Threading.Timer exists, but I already have a Thread. This thread
I have this foreach statement that is supposed to cycle through an array, but
I have simple serial port program that is supposed to read the serial port
I have this piece of code that is supposed to work on Firefox 3.6
I have this function that is supposed to query the database every 4 seconds
I am working with a countdown timer and have it set so that it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.