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

  • Home
  • SEARCH
  • 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 4556530
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:22:45+00:00 2026-05-21T17:22:45+00:00

I am working with some classmates on a game, we have programmed quite everything

  • 0

I am working with some classmates on a game, we have programmed quite everything but the music until now. The music.java class is posted below.

We have a problem stopping our sound, so I need a way to stop this clip from playing so we can actually start another clip, that way we can shift through music during our game (like when you start a game, the song should be different from the main menu).

Even if I can just destroy the object to make a new one, if that’s a possibility, I am willing to do that, but I do not have a clue how to do this. I would rather have a possibility to Stop the current clip file, and replace it with a new one.

package sound;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;


public class Music implements LineListener, Runnable
{

private File soundFile;
private Thread thread;
private static Music player;
private Music audio;
private Clip clip;
private boolean stoppen = false;

/**
* Private because of the singleton
*/
public Music()
{
}

/**
* Play a siren sound
*/
public void playSiren(String musicFileName)
{
    audio = getPlayer();

    audio.playSirenFile(musicFileName);
}

/**
* Play the siren file
*/
private void playSirenFile(String musicFileName)
{
    this.soundFile = new File("Music/"+musicFileName+".wav");
    thread = new Thread(this);
    thread.setName("SoundPlayer");
    thread.start();
}

/**
* Invoked when the thread kicks off
*/
public void run()
{
    try
    {
        AudioInputStream stream =     AudioSystem.getAudioInputStream(this.soundFile);
        AudioFormat format = stream.getFormat();

/**
* we can't yet open the device for ALAW/ULAW playback, convert
* ALAW/ULAW to PCM
*/
            if ((format.getEncoding() == AudioFormat.Encoding.ULAW) ||    (format.getEncoding() == AudioFormat.Encoding.ALAW))
            {
            AudioFormat tmp = new AudioFormat(
                AudioFormat.Encoding.PCM_SIGNED,
            format.getSampleRate(),
            format.getSampleSizeInBits() * 2, format.getChannels(),
            format.getFrameSize() * 2, format.getFrameRate(), true);
            stream = AudioSystem.getAudioInputStream(tmp, stream);
            format = tmp;
        }
        DataLine.Info info = new DataLine.Info(Clip.class, stream
        .getFormat(), ((int) stream.getFrameLength() * format
        .getFrameSize()));

        this.clip = (Clip) AudioSystem.getLine(info);
        this.clip.addLineListener(this);
        this.clip.open(stream);
        this.clip.start();
        try
        {
            thread.sleep(99);
        }
        catch (Exception e)
        {
        }
        while (clip.isActive() && thread != null)
        {
            try
            {
                thread.sleep(99);
            }
            catch (Exception e)
            {
                break;
            }
        }
        clip.loop(999999999);
        clip.drain();


    }
    catch (UnsupportedAudioFileException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    catch (LineUnavailableException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
}

private static Music getPlayer()
{
    if (player == null)
    {
        player = new Music();
    }
    return player;
}

public void update(LineEvent event)
{
}

public void stopClip()
{
    //TODO NEED HELP HERE
}

public void startClip()
{
    //TODO need help here
}
public void volume(float volume)
{

    //TODO NEED HELP HERE
    /*
    FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
    gainControl.setValue(-50.0f); // Reduce volume IN DECIBELS
    clip.start();
        */
    }
}

In my GuiController, I make a new Music object, through the method playSiren I give my songname, and then it automatically runs.

  • 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-05-21T17:22:46+00:00Added an answer on May 21, 2026 at 5:22 pm

    Look into the DataLine.stop() method. Clip implements DataLine.


    E.G. ClipControl.java

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

Sidebar

Related Questions

Working with some basic java apps on CentOS 5 linux and I have my
I have a simple application which was working fine until I added some code
I'm having some trouble with Instruments. My app is working perfectly without crashes, but
I have a working process, behind a web application, that is generating some records
Some time ago I managed to get RoR working on Windows XP. I've been
I am working on an application which has some legacy code. Here, there is
I'm working though SICP and wanted to try out some of the examples in
I'm currently working on a project in which i need to read some (Latitude,
I'm working on a firefox extention. I like to give some options for the
I am working on a traveling salesman problem here and my p-queue isn't operating

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.