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
could not get audio input stream from input file
提问by user3397667
I want to play the mp3
file in java
. I am using a code but show the exception could not get audio input stream from input file.
My source code is :
我想mp3
在java
. 我正在使用代码但显示异常无法从输入文件获取音频输入流。我的源代码是:
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
回答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 JavaFX
jars from here.
如果你不在Java 7
,你可以JavaFX
从这里拿罐子。