2

我正在编写一个小应用程序来播放广播流,并且我正在使用 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 返回 icy 200 ok 标头,其中 Windows 上的某些内容必须希望它返回 http 标头。我似乎无法找到如何让它在使用 javazoom Player 的 Windows 上接受这些不同的标题。

4

1 回答 1

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



}
于 2013-07-06T19:20:23.860 回答