Search in sources :

Example 1 with IcyInputStream

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);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) IcyInputStream(javazoom.spi.mpeg.sampled.file.tag.IcyInputStream) MP3Tag(javazoom.spi.mpeg.sampled.file.tag.MP3Tag)

Example 2 with IcyInputStream

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;
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) IcyInputStream(javazoom.spi.mpeg.sampled.file.tag.IcyInputStream) PushbackInputStream(java.io.PushbackInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IcyInputStream(javazoom.spi.mpeg.sampled.file.tag.IcyInputStream) IOException(java.io.IOException) URLConnection(java.net.URLConnection)

Example 3 with IcyInputStream

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;
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) IcyInputStream(javazoom.spi.mpeg.sampled.file.tag.IcyInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) InputStream(java.io.InputStream) IcyInputStream(javazoom.spi.mpeg.sampled.file.tag.IcyInputStream) IOException(java.io.IOException) URLConnection(java.net.URLConnection)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)3 IcyInputStream (javazoom.spi.mpeg.sampled.file.tag.IcyInputStream)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URLConnection (java.net.URLConnection)2 AudioInputStream (javax.sound.sampled.AudioInputStream)2 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)2 FileInputStream (java.io.FileInputStream)1 PushbackInputStream (java.io.PushbackInputStream)1 MP3Tag (javazoom.spi.mpeg.sampled.file.tag.MP3Tag)1