hello i want to stop the music after pressing other song i tryed much different ways of doing it but no results please anyone help me i have this code ...

package passwordsaver1;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.net.URL;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JOptionPane;

/**
 *
 * @author EXTREME
 */
public final class Music extends javax.swing.JFrame {

    void centreWindow() {
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((dimension.getWidth() - getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - getHeight()) / 2);
        setLocation(x, y);
    }

    /**
     * Creates new form Music
     */
    public Music(String mainAccName) {
        initComponents();
        centreWindow();
        setTitle("PasswordSaver - Music");
        this.mainAccName = mainAccName;
    }
    String mainAccName;
    private void pitbullActionPerformed(java.awt.event.ActionEvent evt) {                                        
        new Thread(new PitBull()).start();
        new Thread(new StartCount()).start();
    }                                       

    private void afrojackActionPerformed(java.awt.event.ActionEvent evt) {                                         
        new Thread(new Afrojack()).start();
        new Thread(new StartCount()).start();
    }                                        

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Music.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Music.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Music.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Music.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                //new Music().setVisible(true);
            }
        });
    }

    private class StartCount implements Runnable {

        @Override
        public void run() {
            for (int x = 0; x < 101; x++) {
                ProgressBar.setValue(x);
                ProgressBar.repaint();
                try {
                    Thread.sleep(180);
                } catch (Exception ex) {
                }
            }
        }
    }

    class Afrojack implements Runnable {

        @Override
        public void run() {
            try {
                URL url = new URL("http://mini-mk-market.com/music/Afrojack.wav");
                Clip clip = AudioSystem.getClip();
                AudioInputStream ais;
                ais = AudioSystem.getAudioInputStream(url);
                clip.open(ais);
                clip.start();
                clip.loop(-1);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Can't find Internet Connection !");
            }
        }
    }

    public void stop() {
        try {
            URL url = new URL("http://mini-mk-market.com/music/PitBulll.wav");
            Clip clip = AudioSystem.getClip();
            AudioInputStream ais;
            ais = AudioSystem.getAudioInputStream(url);
            clip.stop();
        } catch (Exception ex) {
            //Logger.getLogger(Music.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    class PitBull implements Runnable {

        @Override
        public void run() {
            try {
                URL url = new URL("http://mini-mk-market.com/music/PitBulll.wav");
                Clip clip = AudioSystem.getClip();
                AudioInputStream ais;
                ais = AudioSystem.getAudioInputStream(url);
                clip.open(ais);
                clip.loop(-1);
                clip.start();
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Can't find Internet Connection !");
            }
        }
    }

Recommended Answers

All 6 Replies

Anyone help ?

Cmon i need help please some one help me :/

Disclaimer: I haven't used these classes, I'm the opposite of an expert on them, but since nobody else has chipped in and Stefan is getting desparate...

When you start the first Clip playing you use a local variable for the Clip, so when the method terminates you no longer have a reference to the Clip that's paying. Maybe that's why you can't stop it.

Have you tried having a variable in your Music class to hold (a ref to) the currently-playing Clip? Initialise it when you start a Clip playing. Then you can use that in any later methods to stop that Clip.

(obviously not so urgent after all)

I did not have tryed that and i dont know how to do it show please ... :/

I don't mean to be rude, but if you don't know how to declare, set and use a variable in the correct scope then there's no way you should be trying to write a multi-threaded sound program. Maybe you are doing this all in the wrong sequence? It's a mistake to try to go too far too fast. IMHO you should set this to one side, spend a bit more time on the basics, then come back to this when you have the requisite skills. If you don't like this advice then please just ignore it; I'm only trying to say what's best.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.