Question

I'm writing a small app to play a shoutcast stream, and I am using javazoom.jl.player.Player to do this. Here is my code:

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

This works perfectly fine on my Mac and I can hear the stream. However on Windows when I try to run this I get the error "java.io.IOException: Invalid Http response". I believe this is because SHOUTcast returns icy 200 ok headers wherein something on Windows must want it to return http headers. I can't seem to find how to make it accept these different headers on windows using javazoom Player.

Was it helpful?

Solution

I ended up solving this issue by using BasicPlayerListener instead. I replaced the code in my question with the following:

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



}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top