0

我想使用 a 停止音频流JButton,我该怎么做?

这是我的代码:

import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream; 
import java.io.InputStream;

public class twerk {
Thread thread;
static int loo = 1;
static File fl = new File("MYFILE");
public static void frame(){
    JFrame frame = new JFrame("COLLIN");
    frame.setSize(1086,800);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    ImageIcon image = new ImageIcon("MYPICTURE");
    JLabel label = new JLabel(image);
    frame.add(label, BorderLayout.NORTH);
    frame.setIconImage(image.getImage());

    JButton button = new JButton("Stop Sound");
    frame.add(button, BorderLayout.SOUTH);


    frame.setVisible(true);
}


    public static void PlayClip(){
        try
        {

            String st =fl.getPath();
            InputStream in = new FileInputStream(st);
            AudioStream au = new AudioStream(in);
            AudioPlayer.player.start(au);
            if(loo == 1){
                Thread.sleep(1900);
                PlayClip();
            }

        } catch(Exception e){}
    }



    public static void main(String[] args){
      frame();
      PlayClip();
}



   }

我想用这样的 actionlistener 来停止 au:

static class Action implements ActionListener{
       public void actionPreformed (ActionEvent e){
            AudioPlayer.player.stop();
       }

我该怎么做。也许我可以做类似的事情

if(JButton.clicked){
audiostream.player.stop
}

任何投入将不胜感激!

4

1 回答 1

0

尝试将 ActionListener 实现到您的 twerk 类中。然后在您的类中实现 actionPerformed 方法并将侦听器添加到按钮。例如

public class twerk implements ActionListener{
public void twerk() {
    button.addActionListener(this);
// omitted
}

public void actionPerformed(ActionEvent e) {
    JButton tmp = (JButton)e.getSource();
    if(tmp == button)
        au.player.stop();
}

而且最好让你的类名以大写字母开头,比如'Twerk'

祝您申请顺利!

于 2013-06-21T21:32:46.500 回答