0

I am trying to load a MP3 file. I have jmf.jar (windows version) in my classpath and am trying to run my class through Eclipse. But I get this error when trying to run.

I downloaded and set this version of JMF from the oracle site:

JMF2.1.1e\lib

I am running with Java 7 from Oracle (through Eclipse)

Error:

 javax.sound.sampled.UnsupportedAudioFileException: 
    could not get audio input stream from input stream  
    at  
 javax.sound.sampled.AudioSystem.getAudioInputStream
    (Unknown Source)
    at
 org.berlin.sound.WaveformDisplaySimulator.main
    (WaveformDisplaySimulator.java:47)

Here is the code:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

import javax.media.Codec;
import javax.media.Format;
import javax.media.PlugInManager;
import javax.media.format.AudioFormat;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;


    public static void main(final String[] args) {
        try {

            System.out.println(System.getProperty("java.version"));
            final String MP3 = "com.sun.media.codec.audio.mpa.JavaDecoder";
            Codec mp3 = (Codec) Class.forName(MP3).newInstance();

            final Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
            final Format input2 = new AudioFormat(AudioFormat.MPEG);
            final Format output = new AudioFormat(AudioFormat.LINEAR);
            PlugInManager.addPlugIn(
                "com.sun.media.codec.audio.mpa.JavaDecoder",                
                new Format[]{ input1, input2 },
                new Format[]{ output },
                PlugInManager.CODEC
            );

            final AudioFileFormat.Type [] types = AudioSystem.getAudioFileTypes();
            for (final AudioFileFormat.Type t : types) {
                System.out.println("Returning Type : " + t);
            } // End of the for //


            final String PATH = "C:\\Users\\Downloads\\soundcloud2.mp3"; 
            final File file = new File(PATH);
            final AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(file)));

        } catch (final Exception e) {
            e.printStackTrace();
        }
    } // End of the method //
Philip Whitehouse
  • 4,293
  • 3
  • 23
  • 36
Berlin Brown
  • 11,504
  • 37
  • 135
  • 203
  • Have you got the MP3 Library plugin as well? http://www.oracle.com/technetwork/java/javase/download-137625.html – Philip Whitehouse Oct 20 '12 at 00:29
  • `could not get audio input stream from input stream` Not all MP3s are equal. Try it with other MP3s, particularly old/simple ones as might be found [here](http://pscode.org/media/#sound) (the page is slow at the moment). – Andrew Thompson Oct 20 '12 at 00:54
  • I don't see the mp3plugin.jar. When I click on that link above, I get redirected to the JMF download site which doesn't specifically have the mp3plugin. Also, I rand JMStudio and was able to load the MP3. So it must be my code. I don't know what to change. Also, the JavaDecoder class is not in my classpath, I can't find it. – Berlin Brown Oct 20 '12 at 01:02
  • I couldn't get the oracle downloads to work. I downloaded this mp3plugin and it worked without any code changes. Strange that the oracle files wouldn't work. Apparently they may be patent issues with the mp3 drivers? http://www.tritonus.org/plugins.html – Berlin Brown Oct 21 '12 at 00:01

3 Answers3

1

I never could get the oracle download to work. I ended up downloading a MP3 plugin from this site and then adding the plugin in my classpath. This worked with Eclipse and without.

http://www.tritonus.org/plugins.html

Also, I didn't have to modify my code. I was able to read the mp3 binary data and also stream to output.

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

http://www.tritonus.org/plugins.html

    public static void main(final String [] args) throws Exception {
        System.out.println("Running");        
        System.out.println(System.getProperty("java.version"));        
        final AudioFileFormat.Type [] types = AudioSystem.getAudioFileTypes();
        for (final AudioFileFormat.Type t : types) {
            System.out.println("Returning Type : " + t);
        } // End of the for //                
        final String PATH = "C:\\Users\\bbrown\\Downloads\\swing-hacks-examples-20060109\\Ch10-Audio\\75\\soundcloud2.mp3";             
        final File file = new File(PATH);
        final AudioInputStream in = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(file)));

        AudioInputStream din = null;
        final AudioFormat baseFormat = in.getFormat();
        final AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                baseFormat.getSampleRate(),
                16,
                baseFormat.getChannels(),
                baseFormat.getChannels() * 2,
                baseFormat.getSampleRate(),
                false);

        System.out.println("Channels : " + baseFormat.getChannels());                
        din = AudioSystem.getAudioInputStream(decodedFormat, in);        
        rawplay(decodedFormat, din);
        in.close();       
        System.out.println("Done");
    }    

    private static synchronized void rawplay(final AudioFormat targetFormat, final AudioInputStream din) throws IOException, LineUnavailableException {              
        final byte[] data = new byte[4096];
        final SourceDataLine line = getLine(targetFormat);               
        if (line != null) {
            System.out.println("Entering ...");
            // Start
            line.start();
            int nBytesRead = 0, nBytesWritten = 0;
            while (nBytesRead != -1) {
                nBytesRead = din.read(data, 0, data.length);
                if (nBytesRead != -1) {
                    nBytesWritten = line.write(data, 0, nBytesRead);
                    System.out.println("... -->" + data[0] + " bytesWritten:" + nBytesWritten);
                }                                           
            } // End of while //            
            System.out.println("Done ...");
            // Stop
            line.drain();
            line.stop();
            line.close();
            din.close();
        } // End of the if //
    }
Berlin Brown
  • 11,504
  • 37
  • 135
  • 203
0

AudioInputStream can't open mp3 format sounds cause they are compressed , even if you format them as PCM_SIGNED like wav files they can't be played always . You may like jLayer , I have never seen it to crash

import javazoom.jl.player.advanced.AdvancedPlayer;

`

            try{
            File file = new File("C:/DMK.mp3");
            FileInputStream in = new FileInputStream(file);
            AdvancedPlayer player = new AdvancedPlayer(in);
            player.play();
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }

`

Rahul
  • 289
  • 2
  • 7
0

It's 2017, and I added support for mp3 encoding/decoding by using the mp3spi package (http://www.javazoom.net/mp3spi/mp3spi.html)

Somebody was kind enough to set it up as a dependency on Google Code. Here's the gradle entry:

compile group: 'com.googlecode.soundlibs', name: 'mp3spi', version: '1.9.5-1'

So this or the maven equivalent should give you mp3 support in your project.

IcedDante
  • 6,145
  • 12
  • 57
  • 100