Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not get audio input stream from input stream

Tags:

java

javasound

I want to get the sound file at the URL provided in the code and play it (It is in mp3 format). I looked through some Stack Overflow questions related to this problem, and they all said to get mp3plugin.jar so I did.

In Eclipse, I added it as an external jar (as it is located inside of my Downloads folder, not sure if that's the best place for it) under Configure Build Path. I ran it again and it's still giving me this error:

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at Starter.main(Starter.java:21)

Here is the code:

public class Starter {

    public static void main(String[] args) {
        AudioInputStream din = null;
        try {
            URL url = new URL("http://c5.rbxcdn.com/2e6d33a5b3b1d8f250c395816ff9f145");
            HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
            InputStream bufferedIn = new BufferedInputStream(httpcon.getInputStream());
            AudioInputStream in = AudioSystem.getAudioInputStream(bufferedIn);
            AudioFormat baseFormat = in.getFormat();
            AudioFormat decodedFormat = new AudioFormat(
                    AudioFormat.Encoding.PCM_SIGNED,
                    baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
                    baseFormat.getChannels() * 2, baseFormat.getSampleRate(),
                    false);
            din = AudioSystem.getAudioInputStream(decodedFormat, in);
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat);
            SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
            if(line != null) {
                line.open(decodedFormat);
                byte[] data = new byte[4096];
                // Start
                line.start();

                int nBytesRead;
                while ((nBytesRead = din.read(data, 0, data.length)) != -1) {
                    line.write(data, 0, nBytesRead);
                }
                // Stop
                line.drain();
                line.stop();
                line.close();
                din.close();
            }

        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            if(din != null) {
                try { din.close(); } catch(IOException e) { }
            }
        }
    }
}
like image 899
Encoded Lua Avatar asked Jan 23 '16 23:01

Encoded Lua


People also ask

How do I get input stream?

Methods of InputStreamread() - reads one byte of data from the input stream. read(byte[] array) - reads bytes from the stream and stores in the specified array. available() - returns the number of bytes available in the input stream. mark() - marks the position in the input stream up to which data has been read.

What is input stream?

Input Stream : If you are reading data from a file or any other source , stream used is input stream. In a simpler terms input stream acts as a channel to read data. Output Stream : If you want to read and process data from a source (file etc) you first need to save the data , the mean to store data is output stream .

Which class is the superclass of all the input stream class?

Class InputStream. This abstract class is the superclass of all classes representing an input stream of bytes.


1 Answers

You need to get http://www.javazoom.net/mp3spi/docs/doc1.9.4/javazoom/spi/mpeg/sampled/file/MpegAudioFileReader.html

download the jars. I have my classpath as

.;C:\Vision\Audio\libs\vorbisspi1.0.3.jar;C:\Vision\Audio\libs\tritonus_share.jar;C:\Vision\Audio\libs\tritonus_remaining-0.3.6.jar;C:\Vision\Audio\libs\jorbis-0.0.15.jar;C:\Vision\Audio\libs\jogg-0.0.7.jar;C:\Vision\Audio\libs\jl1.0.jar;C:\Vision\Audio\libs\mp3spi1.9.4.jar;

you probably only need mp3spi1.9.4.jar - some of these are for other formats but I'm not sure so I include them all.

Then have the following program

public AudioInputStream readMP3URL(String f) {
    AudioInputStream audioInputStream = null;
    AudioFormat targetFormat = null;
    try {
        AudioInputStream in = null;
        MpegAudioFileReader mp = new MpegAudioFileReader();
        in = mp.getAudioInputStream(new URL(f));
        AudioFormat baseFormat = in.getFormat();
        targetFormat = new AudioFormat(
                AudioFormat.Encoding.PCM_SIGNED,
                baseFormat.getSampleRate(),
                16,
                baseFormat.getChannels(),
                baseFormat.getChannels() * 2,
                baseFormat.getSampleRate(),
                false);
        audioInputStream = AudioSystem.getAudioInputStream(targetFormat, in);
    }
    catch (Exception ue) {
        System.out.println("\nUnsupported Audio");
    }
    return audioInputStream;
}

public void readURL() {
    int i, j, k = 0, l, basicU = 1024;
    AudioFormat targetFormat = null;
    audioInputStream = readMP3URL("http://c5.rbxcdn.com/2e6d33a5b3b1d8f250c395816ff9f145");
    if (audioInputStream == null) System.out.println("null audiostream");
    targetFormat = audioInputStream.getFormat();
    byte[] data = new byte[basicU];
    DataLine.Info dinfo = new DataLine.Info(SourceDataLine.class, targetFormat);
    SourceDataLine line = null;
    try {
        line = (SourceDataLine) AudioSystem.getLine(dinfo);
        if (line != null) {
            line.open(targetFormat);
            line.start();
            while ((k = audioInputStream.read(data, 0, data.length)) != -1) {
                line.write(data, 0, k);
            }
            line.stop();
            line.close();
        }
    }
    catch (Exception ex) {
        ex.printStackTrace();
        System.out.println("audio problem " + ex);
    }
}
like image 125
gpasch Avatar answered Oct 10 '22 22:10

gpasch