0

我正在尝试加载 MP3 文件。我的类路径中有 jmf.jar(windows 版本),并试图通过 Eclipse 运行我的类。但是在尝试运行时出现此错误。

我从 oracle 站点下载并设置了这个版本的 JMF:

JMF2.1.1e\lib

我正在使用 Oracle 的 Java 7(通过 Eclipse)

错误:

 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)

这是代码:

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 //
4

3 回答 3

1

我永远无法让 oracle 下载工作。我最终从这个站点下载了一个 MP3 插件,然后在我的类路径中添加了该插件。这适用于 Eclipse 和没有。

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

此外,我不必修改我的代码。我能够读取 mp3 二进制数据并流式传输到输出。

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 //
    }
于 2012-10-21T06:44:09.613 回答
0

AudioInputStream 无法打开 mp3 格式的声音,因为它们已被压缩,即使您将它们格式化为 PCM_SIGNED (如 wav 文件),它们也无法始终播放。你可能喜欢 jLayer ,我从没见过它崩溃

导入 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();
        }

`

于 2014-06-05T03:39:32.580 回答
0

现在是 2017 年,我通过使用 mp3spi 包 ( http://www.javazoom.net/mp3spi/mp3spi.html )添加了对 mp3 编码/解码的支持

有人好心地将其设置为对 Google Code 的依赖。这是gradle条目:

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

所以这个或 maven 等价物应该在您的项目中为您提供 mp3 支持。

于 2017-01-19T21:45:06.863 回答