use of javazoom.spi.mpeg.sampled.file.tag.IcyInputStream in project Minim by ddf.
the class MpegAudioFileReader method loadShoutcastInfo.
/**
* Load shoutcast (ICY) info.
*
* @param input
* @param props
* @throws IOException
*/
protected void loadShoutcastInfo(InputStream input, HashMap<String, Object> props) throws IOException {
IcyInputStream icy = new IcyInputStream(new BufferedInputStream(input));
// HashMap metadata = icy.getTagHash();
MP3Tag titleMP3Tag = icy.getTag("icy-name");
if (titleMP3Tag != null)
props.put("title", ((String) titleMP3Tag.getValue()).trim());
MP3Tag[] meta = icy.getTags();
if (meta != null) {
// StringBuffer metaStr = new StringBuffer();
for (int i = 0; i < meta.length; i++) {
String key = meta[i].getName();
String value = ((String) icy.getTag(key).getValue()).trim();
props.put("mp3.shoutcast.metadata." + key, value);
}
}
}
use of javazoom.spi.mpeg.sampled.file.tag.IcyInputStream in project Minim by ddf.
the class MpegAudioFileReader method getAudioInputStream.
/**
* Returns AudioInputStream from url.
*/
public AudioInputStream getAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException {
system.debug("MpegAudioFileReader.getAudioInputStream(URL): begin");
long lFileLengthInBytes = AudioSystem.NOT_SPECIFIED;
URLConnection conn = url.openConnection();
// Tell shoucast server (if any) that SPI support shoutcast stream.
boolean isShout = false;
int toRead = 4;
byte[] head = new byte[toRead];
conn.setRequestProperty("Icy-Metadata", "1");
BufferedInputStream bInputStream = new BufferedInputStream(conn.getInputStream());
bInputStream.mark(toRead);
int read = bInputStream.read(head, 0, toRead);
if ((read > 2) && (((head[0] == 'I') | (head[0] == 'i')) && ((head[1] == 'C') | (head[1] == 'c')) && ((head[2] == 'Y') | (head[2] == 'y'))))
isShout = true;
bInputStream.reset();
InputStream inputStream = null;
// Is is a shoutcast server ?
if (isShout == true) {
// Yes
IcyInputStream icyStream = new IcyInputStream(bInputStream);
icyStream.addTagParseListener(IcyListener.getInstance());
inputStream = icyStream;
} else {
// No, is Icecast 2 ?
String metaint = conn.getHeaderField("icy-metaint");
if (metaint != null) {
// Yes, it might be icecast 2 mp3 stream.
IcyInputStream icyStream = new IcyInputStream(bInputStream, metaint);
icyStream.addTagParseListener(IcyListener.getInstance());
inputStream = icyStream;
} else {
// No
inputStream = bInputStream;
}
}
AudioInputStream audioInputStream = null;
try {
audioInputStream = getAudioInputStream(inputStream, lFileLengthInBytes);
} catch (UnsupportedAudioFileException e) {
inputStream.close();
throw e;
} catch (IOException e) {
inputStream.close();
throw e;
}
system.debug("MpegAudioFileReader.getAudioInputStream(URL): end");
return audioInputStream;
}
use of javazoom.spi.mpeg.sampled.file.tag.IcyInputStream in project Minim by ddf.
the class MpegAudioFileReaderWorkaround method getAudioInputStream.
/**
* Returns AudioInputStream from url and userAgent
*/
public AudioInputStream getAudioInputStream(URL url, String userAgent) throws UnsupportedAudioFileException, IOException {
system.debug("MpegAudioFileReaderWorkaround.getAudioInputStream(" + url.toString() + ", " + userAgent + "): begin");
long lFileLengthInBytes = AudioSystem.NOT_SPECIFIED;
URLConnection conn = url.openConnection();
// Tell shoucast server (if any) that SPI support shoutcast stream.
boolean isShout = false;
int toRead = 4;
byte[] head = new byte[toRead];
if (userAgent != null)
conn.setRequestProperty("User-Agent", userAgent);
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Icy-Metadata", "1");
conn.setRequestProperty("Connection", "close");
system.debug("Base input stream is: " + conn.getInputStream().toString());
BufferedInputStream bInputStream = new BufferedInputStream(conn.getInputStream());
bInputStream.mark(toRead);
int read = bInputStream.read(head, 0, toRead);
if ((read > 2) && (((head[0] == 'I') | (head[0] == 'i')) && ((head[1] == 'C') | (head[1] == 'c')) && ((head[2] == 'Y') | (head[2] == 'y')))) {
isShout = true;
}
bInputStream.reset();
InputStream inputStream = null;
// Is it a shoutcast server ?
if (isShout == true) {
// Yes
system.debug("URL is a shoutcast server.");
IcyInputStream icyStream = new IcyInputStream(bInputStream);
icyStream.addTagParseListener(IcyListener.getInstance());
inputStream = icyStream;
} else {
// No, is it Icecast 2 ?
String metaint = conn.getHeaderField("icy-metaint");
if (metaint != null) {
// Yes, it might be icecast 2 mp3 stream.
system.debug("URL is probably an icecast 2 mp3 stream");
IcyInputStream icyStream = new IcyInputStream(bInputStream, metaint);
icyStream.addTagParseListener(IcyListener.getInstance());
inputStream = icyStream;
} else {
system.debug("URL is not shoutcast or icecast 2.");
inputStream = bInputStream;
}
}
AudioInputStream audioInputStream = null;
try {
system.debug("Attempting to get audioInputStream.");
audioInputStream = getAudioInputStream(inputStream, lFileLengthInBytes);
} catch (UnsupportedAudioFileException e) {
inputStream.close();
throw e;
} catch (IOException e) {
inputStream.close();
throw e;
}
system.debug("MpegAudioFileReaderWorkaround.getAudioInputStream(URL,String): end");
return audioInputStream;
}
Aggregations