// ShoutcastStreamer(String urlAsString) { // try { // URL url = new URL(urlAsString); // URLConnection uc = url.openConnection(); // uc.addRequestProperty("Icy-MetaData", "1"); // // //get all headers // Map<String, List<String>> map = uc.getHeaderFields(); // for (Map.Entry<String, List<String>> entry : map.entrySet()) { // System.out.println("Key : " + entry.getKey() // + " ,Value : " + entry.getValue()); // } // // System.out.println(map.size()); // //metaint = Integer.parseInt(uc.getHeaderField("icy-metaint")); // metaint = 32768; // System.out.println(metaint); // // InputStream in = uc.getInputStream(); // byte[] buffer = new byte[metaint]; // for (int i = 0; i < buffer.length; i++) { // buffer[i] = (byte) in.read(); // } // int metaLength = in.read() * 16; // String metaData = new String(); // for (int i = 0; i < metaLength; i++) { // metaData = metaData.concat(String.valueOf((char) in.read())); // } // System.out.println(metaData); // // } catch (IOException ex) { // Logger.getLogger(ShoutcastStreamer.class.getName()).log(Level.SEVERE, null, ex); // } // } private void playInputStream(InputStream stream) { try { if (player != null) { player.close(); } player = new Player(stream); player.play(); } catch (Exception ex) { Logger.getLogger(ShoutcastPlayer.class.getName()).log(Level.SEVERE, null, ex); } }
public void songManager() { UI ui = new UI(); int choice = ui.getChoice(); Play p = new Play(this); while (choice != 5) { switch (choice) { case 1: sortByArtist(); print(); break; case 2: sortByTitle(); print(); break; case 3: Scanner sc = new Scanner(new InputStreamReader(System.in)); int songChoice = 0; while (songChoice < 1 || songChoice > size()) { System.out.println("Enter Song Choice: "); sortByArtist(); print(); songChoice = sc.nextInt(); } try { p.playSong(songChoice, this); } catch (JavaLayerException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; case 4: if (player != null) { player.close(); } break; default: break; } choice = ui.getChoice(); if (choice == 5) break; } }
@ActionDoc(text = "plays an audio stream from an url") public static synchronized void playStream( @ParamDoc(name = "url", text = "the url of the audio stream") String url) { if (streamPlayer != null) { // if we are already playing a stream, stop it first streamPlayer.close(); streamPlayer = null; } if (url == null) { // the call was only for stopping the currently playing stream return; } try { if (url.toLowerCase().endsWith(".m3u")) { InputStream is = new URL(url).openStream(); String urls = IOUtils.toString(is); for (String line : urls.split("\n")) { if (!line.isEmpty() && !line.startsWith("#")) { url = line; break; } } } else if (url.toLowerCase().endsWith(".pls")) { InputStream is = new URL(url).openStream(); String urls = IOUtils.toString(is); for (String line : urls.split("\n")) { if (!line.isEmpty() && line.startsWith("File")) { Matcher matcher = plsStreamPattern.matcher(line); if (matcher.find()) { url = matcher.group(1); break; } } } } URL streamUrl = new URL(url); URLConnection connection = streamUrl.openConnection(); InputStream is = null; if (connection.getContentType().equals("unknown/unknown")) { // Java does not parse non-standard headers used by SHOUTCast int port = streamUrl.getPort() > 0 ? streamUrl.getPort() : 80; // Manipulate User-Agent to receive a stream shoutCastSocket = new Socket(streamUrl.getHost(), port); OutputStream os = shoutCastSocket.getOutputStream(); String user_agent = "WinampMPEG/5.09"; String req = "GET / HTTP/1.0\r\nuser-agent: " + user_agent + "\r\nIcy-MetaData: 1\r\nConnection: keep-alive\r\n\r\n"; os.write(req.getBytes()); is = shoutCastSocket.getInputStream(); } else { is = streamUrl.openStream(); } if (is != null) { Player player = new Player(is); streamPlayer = player; playInThread(player); } } catch (JavaLayerException e) { logger.error("Cannot play stream '{}': JavaLayerException - {}", url, e.getMessage()); } catch (MalformedURLException e) { logger.error("Cannot play stream '{}': MalformedURLException - {}", url, e.getMessage()); } catch (IOException e) { logger.error("Cannot play stream '{}': {}", url, e); } }
/** Stops the song. You cannot restart it afterwards. */ public void stopSong() { music.close(); }
void stop() { player.close(); }