Java 无法从输入文件中获取音频输入流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22344020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:04:02  来源:igfitidea点击:

could not get audio input stream from input file

javamp3

提问by user3397667

I want to play the mp3file in java. I am using a code but show the exception could not get audio input stream from input file. My source code is :

我想mp3java. 我正在使用代码但显示异常无法从输入文件获取音频输入流。我的源代码是:

try {
        System.out.println("Start");
        File f = new File("E:\malayalam good song\01_ISHTAMANU.MP3");

        AudioInputStream audio = AudioSystem.getAudioInputStream(f);
        System.out.println("Start");
        AudioFormat format = audio.getFormat();
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
        SourceDataLine  auline = (SourceDataLine) AudioSystem.getLine(info);
        auline.open(format);
        auline.start();
        int nBytesRead = 0;
        byte[] abData = new byte[524288];
        while (nBytesRead != -1) {
            nBytesRead = audio.read(abData, 0, abData.length);
            if (nBytesRead >= 0) {
                auline.write(abData, 0, nBytesRead);
            }
        }
    } catch (Exception E) {
        System.out.println("Exception"+E.getMessage());
    }

回答by JavaLearner

Add mp3plugin.jarin your classpath.

在类路径中添加mp3plugin.jar

http://pscode.org/lib/mp3plugin.jar

http://pscode.org/lib/mp3plugin.jar

回答by JavaLearner

If you are on java 7, there are new (JavaFX) classes there that are easier to use:

如果您在java 7,那里JavaFX有更易于使用的新 ( ) 类:

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
...
...

try {
        File f = new File("E:\malayalam good song\01_ISHTAMANU.MP3");
        Media hit = new Media(f.toURI().toString());
        MediaPlayer mediaPlayer = new MediaPlayer(hit);
        mediaPlayer.play();
    } catch(Exception ex) {
        ex.printStackTrace();
        System.out.println("Exception: " + ex.getMessage());
    }

If you are not on Java 7, you can grab JavaFXjars from here.

如果你不在Java 7,你可以JavaFX这里拿罐子。