Simone Michaels
Simone Michaels

Reputation: 31

How do I make my program repeat the sound every five seconds?

Here's my code:

import java.awt.*;
import java.awt.event.*
import javax.swing.*;

public class Morning extends JFrame
implements ActionListener

{
  private EasySound rooster;
  private int time;

  public Morning()
    super("Morning");
    rooster = new EasySound("roost.wav");
    rooster.play();

    time = 0;
    Timer clock = new Timer(5000, this);
    clock.start();

    Container c = getContentPane();
    c.setBackground(Color.WHITE);
  }

  public static void main(String[] args)
  {
    Morning morning = new Morning();
    morning.setSize(300, 150);
    morning.setDefaultCloseOperation(EXIT_ON_CLOSE);
    morning.setVisible(true);
  }

  public void actionPerformed(ActionEvent e)
  {
    time++;
  }
}

So my question is, how do I make the roost.wav sound play every five seconds. The program compiles, but it doesn't replay after it plays once.

Thanks to whoever helps! Simone

Upvotes: 0

Views: 809

Answers (2)

user3270760
user3270760

Reputation: 1504

You only play the sound when you create a Morning object, and since you only create a single Morning object in your code, the sound only plays once. You either need to create more Morning objects, or move the code that plays the sound to the actionPerformed() method.

Upvotes: 0

jjlema
jjlema

Reputation: 850

It is the method actionperformed where you have to put the code that plays the sound. Because its what the timer do, it calls actionperformed periodically.

Upvotes: 3

Related Questions