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 7961551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:04:22+00:00 2026-06-04T05:04:22+00:00

I want to create 2 JMenuItem that can start and stop the background audio.

  • 0

I want to create 2 JMenuItem that can start and stop the background audio.

Here is my code :

public class MainClass extends JFrame
{
    private AudioInputStream audioInputStream;
    private Clip clip;

    public MainClass(String title)
    {
        try
        {
            audioInputStream = AudioSystem.getAudioInputStream(new File("Background.wav"));
            clip = AudioSystem.getClip();
            clip.loop(Clip.LOOP_CONTINUOUSLY);
            clip.open(audioInputStream);
        }
        catch(Exception e)
        {
            System.out.println("Error with playing sound.");
            e.printStackTrace();
        }
    }
    public void startSound()
    {   
        clip3.start();
        settingSubMenuItem1.setEnabled(false);
        settingSubMenuItem2.setEnabled(true);
    }

    public void stopSound()
    {
        clip3.stop();
        settingSubMenuItem1.setEnabled(true);
        settingSubMenuItem2.setEnabled(false);
    }

    private class MenuItemListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() == settingSubMenuItem1)
            {
                startSound();
            }
            if(e.getSource() == settingSubMenuItem2)
            {
                stopSound();        
            }
        }
    }
}

When I click the settingSubMenuItem1, it’s work fine, audio is played.

But when I click the settingSubMenuItem2, there is errors and if click again settingSubMenuItem1, there will no more sound.

Here is the errors :

Error with playing sound.
java.lang.IllegalStateException: Clip is already open with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian and frame lengh of 7658

What is the error of my program?

  • 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-04T05:04:24+00:00Added an answer on June 4, 2026 at 5:04 am

    This SSCCE is a ‘null result’ here, in that the audio restarts (tried at least 3 times) with no exceptions.

    import java.net.URL;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.sound.sampled.*;
    
    public class RestartableLoopSound {
    
        public static void main(String[] args) throws Exception {
            URL url = new URL(
                "http://pscode.org/media/leftright.wav");
            final Clip clip = AudioSystem.getClip();
            AudioInputStream ais = AudioSystem.
                getAudioInputStream( url );
            clip.open(ais);
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    final JToggleButton b = new JToggleButton("Loop");
                    ActionListener listener = new ActionListener() {
                        public void actionPerformed(ActionEvent ae) {
                            if (b.isSelected()) {
                                // loop continuously
                                clip.loop(Clip.LOOP_CONTINUOUSLY);
                            } else {
                                clip.stop();
                            }
                        }
                    };
                    b.addActionListener(listener);
                    JOptionPane.showMessageDialog(null, b);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a newbie type question. I want to create a public method that
I want create wordpress website into which I want create user management... That means
i want create a custom json data from the mssql 2008 results so that
I want create an application with animate button? how can i do? after click
i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
I want to create a new field (or two) in my table that is
How can I give my JMenuItem s names that the ActionListener attached to them
I have a MainWindow class that extends JFrame and is the only frame in
This is my problem: class main extends menuActivity{ // .. // public void onActivityResult(int
I am trying to create a JMenuItem that is disabled by default, but a

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.