Java Swing Worker没有停止

时间:2015-03-08 20:41:10

标签: java swing swingworker jlayer

我正在使用Java swing构建应用程序。该应用程序基本上从Broadcastify网站抓取警察无线电流并播放它们。

该分钟的申请有两个按钮:“芝加哥警察”,“芝加哥消防和EMS”。当芝加哥​​警察按钮被按下时,芝加哥警察流开始播放。但是,当按下相同的按钮或不同的按钮时,流不会停止,它们只是自己玩。我认为这与SwingWorker有关。

基本上,我想要它做的是播放流,当按下另一个流按钮时,停止当前流然后启动新流。

我尝试过使用.cancel(true)方法,但这不起作用。

这是我的代码 -

框架类:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class frame extends JFrame{

private JPanel north, south;
private JButton chicagoPolice, chicagoFireEms;
private JTextField status;
stream s = new stream();

public frame(){
    //set title
    super("Broadcastify Streams");

    //north panel
    north = new JPanel();
    north.setPreferredSize(new Dimension(500,250));
    north.setBackground(Color.RED);
    add(north, BorderLayout.NORTH);

    //Chicago Police button
    chicagoPolice = new JButton("Chicago Police");
    north.add(chicagoPolice);

    //Chicago Fire and EMS
    chicagoFireEms  = new JButton("Chicago Fire and EMS");
    north.add(chicagoFireEms);

    //south panel
    south = new JPanel();
    south.setBackground(Color.BLUE);
    add(south, BorderLayout.SOUTH);

    //status bar
    status = new JTextField(15);
    status.setEditable(false);
    south.add(status, BorderLayout.NORTH);

    //button action listeners
    chicagoPolice.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            s.setStream("http://relay.broadcastify.com:80/il_chicago_police2");
            s.playStream();
            status.setText("Playing - Chicago Police");
        }
    });

    chicagoFireEms.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            s.setStream("http://relay.broadcastify.com:80/836454979");
            s.playStream();
            status.setText("Playing - Chicago Fire and EMS");
        }
    });
}
}

stream class:

import java.net.URL;
import java.net.URLConnection;

import javax.swing.SwingWorker;

import javazoom.jl.player.Player;

public class stream {

private SwingWorker<Void,Void> worker;
private String stream;
private Player player;

public stream(){
    stream = "";
}

public void setStream(String radioStream){
    stream = radioStream;
}

public void playStream(){

    worker = new SwingWorker<Void,Void>(){
        public Void doInBackground() throws Exception{

            URLConnection urlConnection = new URL(stream).openConnection();
            urlConnection.connect();
            player = new Player(urlConnection.getInputStream());
            player.play();

            return null;
        }
    };
    worker.execute();
}

}

主要课程:

import javax.swing.JFrame;

public class main {

public static void main(String[] args) {

    frame f = new frame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(500,500);
    f.setResizable(false);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

}

有人可以把它放到Eclipse中测试它,看看我在说什么。您还需要在此处找到的JLayer库:http://www.javazoom.net/javalayer/javalayer.html

感谢。

1 个答案:

答案 0 :(得分:2)

你必须停止玩家,而不是工人。像这样:

import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.*;

public class FooPlayer extends JPanel {
   private PlayerAction[] actions = {
         new PlayerAction(this, "Police",
               "http://relay.broadcastify.com:80/il_chicago_police2"),
         new PlayerAction(this, "Fire", "http://relay.broadcastify.com:80/836454979") };

   public FooPlayer() {
      for (PlayerAction action : actions) {
         add(new JButton(action));
      }
   }

   public PlayerAction[] getActions() {
      return actions;
   }

   private static void createAndShowGui() {
      FooPlayer mainPanel = new FooPlayer();

      JFrame frame = new JFrame("FooPlayer");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class PlayerAction extends AbstractAction {
   private String path;
   private PlayerWorker playerWorker;
   private FooPlayer fooPlayer;

   public PlayerAction(FooPlayer fooPlayer, String name, String path) {
      super(name);
      this.path = path;
      this.fooPlayer = fooPlayer;
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      for (PlayerAction action : fooPlayer.getActions()) {
         action.closePlayer();
      }

      try {
         URLConnection urlConnection = new URL(path).openConnection();
         playerWorker = new PlayerWorker(urlConnection);
         playerWorker.execute();

      } catch (MalformedURLException e1) {
         e1.printStackTrace();
      } catch (IOException e1) {
         e1.printStackTrace();
      }
   }

   private void closePlayer() {
      if (playerWorker != null) {
         playerWorker.closePlayer();
      }
   }
}

class PlayerWorker extends SwingWorker<Void, Void> {
   private Player player;

   public PlayerWorker(URLConnection urlConnection) throws IOException {
      urlConnection.connect();
      player = new Player(urlConnection.getInputStream());
   }

   @Override
   protected Void doInBackground() throws Exception {
      player.play();
      return null;
   }

   public void closePlayer() {
      if (player != null) {
         player.close();
      }
   }
}

关键是必须停止正在运行的播放器,而不是SwingWorker,它将自行停止,而是SwingWorker正在运行的Player对象。