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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:44:40+00:00 2026-06-04T04:44:40+00:00

I have inserted a background audio into my program using AudioInputStream . When I

  • 0

I have inserted a background audio into my program using AudioInputStream.

When I press the On, the background audio will play, and Off will stop playing the background audio.

Here is the part of my code for the audio playing part :

public void playSound(int i)
{
    try 
    {
        .  //Others buttons sound
        .  //Others buttons sound
    if(i == 3 || i == 4)
    {
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("Background.wav"));
        Clip clip3 = AudioSystem.getClip( );
        clip3.open(audioInputStream);
        if(i == 3)
        {
            clip3.loop(Clip.LOOP_CONTINUOUSLY);
            clip3.start();
            settingSubMenuItem3.setEnabled(false);
            settingSubMenuItem4.setEnabled(true);
        }
        if(i == 4)
        {
            clip3.stop();
            settingSubMenuItem3.setEnabled(true);
            settingSubMenuItem4.setEnabled(false);
        }
    }
    catch(Exception e)
    {
        System.out.println("Error with playing sound.");
        e.printStackTrace();
    }
}

And here is the ButtonListener part :

private class MenuItemListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == settingSubMenuItem3)
        {
            playSound(3);
        }
        if(e.getSource() == settingSubMenuItem4)
        {
            playSound(4);       
        }
    }
}

My code can play the audio when I press On button, but when I press Off button, it does not work.

There is no error during compile and run.

What is the error of my code?

  • 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-04T04:44:41+00:00Added an answer on June 4, 2026 at 4:44 am

    Your AudioInputStream variable, audioInputStream, and Clip variable, clip3, are local to the method. The object you are trying to stop is not the same as the one that’s playing currently.

    Make them class fields, check that they’re not null before calling methods on them, don’t create a new object if you’re trying to stop the currently playing object, and you should be OK.

    Something like:

    public void playSound(int i) {
      try {
         // ...
         if (i == 3 || i == 4) {
            if (i == 3) {
               audioInputStream = AudioSystem
                     .getAudioInputStream(new File("Background.wav"));
               clip3 = AudioSystem.getClip();
               clip3.open(audioInputStream);
               clip3.loop(Clip.LOOP_CONTINUOUSLY);
               clip3.start();
               settingSubMenuItem3.setEnabled(false);
               settingSubMenuItem4.setEnabled(true);
            }
            if (i == 4) {
               if (clip3 != null && clip3.isActive()) {
                  clip3.stop();
                  settingSubMenuItem3.setEnabled(true);
                  settingSubMenuItem4.setEnabled(false);
               }
            }
         }
      } catch (Exception e) {
         System.out.println("Error with playing sound.");
         e.printStackTrace();
      }
    }
    

    Again, making audioInputStream and clip3 non-static class fields.

    As an aside, I would avoid using “magic” numbers such as 3 and 4 as you are doing as this may become a devil to debug 6 months from now. Instead give each JButton its own Action. It deserves no less.

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

Sidebar

Related Questions

I have a textarea whose value will be inserted into a mysql database. To
I have a iframe on my page. I have inserted a background image on
I have inserted the following line into my Joomla template: <jdoc:include type=component /> This
i am using a website application for registration, in that application i have inserted
I have a DropDownList as a custom control inserted into a subform which is
I have created a parent div and inserted div's after the parent div using
Background: we have a Grails 1.3.7 app and are using Liquibase to manage our
Background: We have large flat files span around 60GB and are inserting into database.
I have found this code to watch for a CD to be inserted/ejected using
Some of our users have been having css inserted into their posts without their

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.