gpt4 book ai didi

Java Swing Worker 没有停止

转载 作者:行者123 更新时间:2023-12-02 04:54:12 26 4
gpt4 key购买 nike

我正在使用 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");
}
});
}
}

流类:

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

谢谢。

最佳答案

您必须停止玩家,而不是 worker 。像这样的事情:

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();
}
}
}

关键是必须停止正在运行的 Player,而不是 SwingWorker(它会自行停止),而是 SwingWorker 正在运行的 Player 对象。

关于Java Swing Worker 没有停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28931654/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com