javazoom Player不能在带有SHOUTcast流的Windows上工作(表示无效的Https请求)

时间:2012-02-17 17:05:11

标签: java stream shoutcast

我正在编写一个小应用程序来播放shoutcast流,我正在使用javazoom.jl.player.Player来执行此操作。这是我的代码:

package music;

import java.io.InputStream;
import java.net.URL;
import javazoom.jl.player.Player;
class audiostream extends Thread {

private Player mediafilePlayer;
private volatile boolean shouldPlay = true;

@Override
public void run() {
    while (true) {
        if (shouldPlay) {
            player();
        }
    }
}

public void player() {
    try {
        URL mediafile = new URL("http://hi1.streamingsoundtracks.com:8000/;");
        InputStream stream = mediafile.openStream();

        mediafilePlayer = new Player(stream);
        mediafilePlayer.play();
    } catch (Exception e) {
        System.out.println(e);
    }
}

public void pause() {
    shouldPlay = false;
    mediafilePlayer.close();
}

public void play() {
    shouldPlay = true;
}
}

这在我的Mac上工作得很好,我可以听到流。但是在Windows上,当我尝试运行它时,我收到错误“java.io.IOException:无效的Http响应”。我相信这是因为SHOUTcast返回了200个ok标题,其中Windows上的内容必须要求它返回http标头。我似乎无法找到如何使用javazoom Player在Windows上接受这些不同的标题。

1 个答案:

答案 0 :(得分:0)

我最后使用BasicPlayerListener来解决此问题。我用以下内容替换了我的问题中的代码:

package music;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javazoom.jlgui.basicplayer.BasicController;
import javazoom.jlgui.basicplayer.BasicPlayer;
import javazoom.jlgui.basicplayer.BasicPlayerEvent;
import javazoom.jlgui.basicplayer.BasicPlayerException;
import javazoom.jlgui.basicplayer.BasicPlayerListener;

public class audiostream implements BasicPlayerListener, Runnable {

    public String streamurl;
    public BasicController playerControl;
    private volatile boolean shouldPlay = true;

    @Override
    public void run() {
        while (true) {
            if (shouldPlay) {
                player();
            }
        }
    }

    // ** RUN ONCE TO START THREAD
    public void start() {

        new Thread(this).start();

    }

    // ** RUN TO PAUSE/STOP THE PLAYER
    public void pause() {

        // set play bool to false
        shouldPlay = false;


        // stop player
        try {
            playerControl.stop();
        } catch (BasicPlayerException ex) {
            Logger.getLogger(audiostream.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    // ** RUN TO PLAY
    public void play() {
        shouldPlay = true;
    }


    // construct
    public audiostream(String givenStreamurl) {

        // assign the radio url
        streamurl = givenStreamurl;

    }

    // OPENS UP THE SHOUTCAST STREAM
    public void player() {

        // dont allow multiple runnings of this
        shouldPlay = false;

        // start stream
        try {
            BasicPlayer player = new BasicPlayer();
            playerControl = (BasicController) player;
            player.addBasicPlayerListener(this);
            try {
                playerControl.open(new URL(streamurl));
            } catch (MalformedURLException ex) { }
            playerControl.play();
        } catch (BasicPlayerException ex) { }

    }

    @Override
    public void opened(Object o, Map map) {
        //System.out.println("opened : "+map.toString());
    }

    @Override
    public void progress(int i, long l, byte[] bytes, Map map) {
        //System.out.println("opened : "+map.toString());
    }

    @Override
    public void stateUpdated(BasicPlayerEvent bpe) {
        //System.out.println("opened : "+bpe.toString());
    }

    @Override
    public void setController(BasicController bc) {
        //System.out.println("opened : "+bc.toString());
    }



}