关闭它的JavaZoom播放器问题

时间:2018-01-30 13:27:32

标签: java eclipse radio

我正在写一个互联网广播应用程序。我在一个线程中运行JavaZoom Player但我无法从主框架关闭播放器。我总是得到一个NullPointerException。以下是播放器的代码:

import java.net.*;
import java.util.Scanner;
import java.io.*;
import java.net.*;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import javax.*;
import javax.swing.ImageIcon;

public class Main extends Thread {
    public static URL radiourl;
    public Player play;
    public static int art = 0;
    public static String radio = null;
    InputStream stream;

    public void run() {
        int valuehere = RadioGui.value;
        switch (valuehere) {
        case 1:
            radio = "http://mp3-live.swr3.de/swr3_m.m3u";
            art = 1;
            break;
        case 2:
            radio = "http://www.wdr.de/wdrlive/media/einslivedigi.m3u";
            art = 1;
            break;
        case 3:
            radio = "http://www.wdr.de/wdrlive/media/wdr2.m3u";
            art = 1;
            break;
        case 4:
            radio = "http://sunshinelive.hoerradar.de/sunshinelive-live-mp3-hq";
            art = 2;
            break;
        default:
        }
        if (art == 1) {
            try {
                URL url = new URL(radio);
                System.out.println(url);
                Scanner scan = new Scanner(url.openStream());
                String radiourl;
                String radioendurl = null;
                while (scan.hasNext()) {
                    radiourl = scan.nextLine();
                    if (radiourl.contains(".mp3")) {
                        radioendurl = radiourl;
                    }
                }
                url = new URL(radioendurl);
                stream = url.openStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (art == 2) {
            try {
                URL url = new URL(radio);
                stream = url.openStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            play = new Player(stream);
            RadioGui.isRunning = true;
            System.out.println(RadioGui.isRunning);
            play.play();
        } catch (JavaLayerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

在真正的Main中(是的我知道这个类叫做Main但它不是真正的Main ...)我无法关闭播放器(当我输入m(Main).play.close();. 导致错误的代码:

label.addMouseListener(new MouseAdapter() {
        @SuppressWarnings("deprecation")
        @Override
        public void mouseClicked(MouseEvent arg0) {
            sendername = (String)sender.getSelectedItem();
            if(sendername.equals("SWR 3")){
                value = 1;
                bild.setIcon(new ImageIcon(RadioGui.class.getResource("/Images/swr 3.png")));
                name.setText(sendername);
            }
            if(sendername.equals("1 Live DIGGi")){
                value = 2;
                bild.setIcon(new ImageIcon(RadioGui.class.getResource("/Images/1livediggi.png")));
                name.setText(sendername);
            }
            if(sendername.equals("WDR 2")){
                value = 3;
                bild.setIcon(new ImageIcon(RadioGui.class.getResource("/Images/wdr2.gif")));
                name.setText(sendername);
            }
            if(sendername.equals("SUNSHINE LIVE")){
                value = 4;
                bild.setIcon(new ImageIcon(RadioGui.class.getResource("/Images/sunshine.png")));
                name.setText(sendername);
            }
            Main m = new Main();
            Thread y = new Thread(m);
            if(isRunning==true){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {

                }
                y.stop();
                m.play.close();//Here is the error
                System.out.println(y.isAlive());
                System.out.println(m.play.isComplete());
                isRunning = false;
                y.start();
            }
            else{
            y.start();
            }
        }
    });

0 个答案:

没有答案