0

This is what I've tried and it also works for the downloaded file but not for streaming from URL.

public class audioPlayer{

BufferedInputStream BIS;
URL url;
String path = ""; /* Url Address of the music file somewebsite.com/somesong.mp3 */


public void playFromUrl(){

        try {
            url = new URL(path);
            BIS = new BufferedInputStream(url.openStream());

            player = new Player(BIS);
            songTotalLength = url.openStream().available();
            //System.out.println(songTotalLength);

        } catch (Exception e) {
            e.printStackTrace();
        }

        new Thread(){
            public void run(){
                try {
                    player.play();
                } catch (JavaLayerException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public void pause(){

        if (player!=null) {
            try {
                pauseLocation = url.openStream().available();
                System.out.println(pauseLocation);
                player.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void resumeFromURL() {

        try {
            InputStream IS = new URL(path).openStream();
            IS.skip(songTotalLength-pauseLocation);
            BIS = new BufferedInputStream(IS);

            player = new Player(BIS);

        } catch (Exception e) {
            e.printStackTrace();
        }

        new Thread(){
            @Override
            public void run(){
                try {
                    player.play();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
Stultuske
  • 9,296
  • 1
  • 25
  • 37

0 Answers0