import krister.Ess.*; import javax.xml.parsers.*; import org.w3c.dom.*; class SoundBox { String podcast_url; AudioStream audio_stream = null; AudioFile audio_file = null; String audio_stream_url = ""; SoundBox(String podcast_url) { this.podcast_url = podcast_url; if (!podcast_url.equals("")) { playRandomSong(); } } void playRandomSong() { if (audio_file != null) { audio_file.close(); audio_stream.stop(); } audio_stream_url = getRandomSongURL(); if (audio_stream_url.equals("")) { return; } audio_file = new AudioFile(audio_stream_url, 0, Ess.READ); // create a new AudioStream and set the sample rate audio_stream = new AudioStream(32*1024); // 32k samples audio_stream.sampleRate(audio_file.sampleRate); audio_stream.start(); println(audio_stream_url); } String getRandomSongURL() { URL url = null; try { url = new URL(podcast_url); } catch(Exception e) { println(e); return ""; } InputStream is = null; try { is = url.openStream(); } catch(Exception e) { println(e); return ""; } DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = null; try { db = dbf.newDocumentBuilder(); } catch(Exception e) { println(e); return ""; } Document d = null; try { d = db.parse(is); } catch(Exception e) { println(e); return ""; } NodeList songs = d.getElementsByTagName("item"); Node song = songs.item((int)random(songs.getLength())); NodeList song_info = song.getChildNodes(); Node song_i; for(int i = 0; i < song_info.getLength(); i++) { song_i = song_info.item(i); if (song_i.getNodeName().equals("link")) { Node song_link = (song_i.getChildNodes()).item(0); return song_link.getNodeValue(); } } return ""; } boolean isActive() { return (podcast_url != null && !podcast_url.equals("") && audio_stream_url != null && !audio_stream_url.equals("") && audio_file != null); } }