How to stop a music Clip?

后端 未结 1 695
忘掉有多难
忘掉有多难 2020-12-04 03:03

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 sto

相关标签:
1条回答
  • 2020-12-04 03:55

    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 讨论(0)
提交回复
热议问题