Reputation: 11
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 :
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());
}
Upvotes: 1
Views: 13944
Reputation: 67
Dudes, I passed a night matter with this exception, man it was hard to find a solution, first of all, you will not need to alter your code or same thing like that at majority of the cases, you just need to convert the format of wav that you have, following this tutorial: here
But the thread started here
After that I had another problem, basically it was saying that I can't play the sound because iceadtea-sound was not in library and yeah man, to solve that I runned a command:
apt-get install openjdk-8-jre openjdk-8-jdk
that I get on here
And after this bug solved I come across another one named "Invalid format with getAudioInputStream, trying to play a sound in Java"
That bug I solved with this answer here on stackoverflow here
And after this, finally a song was played
Upvotes: 0
Reputation: 1233
You should convert wav audio files to 8khz, 16 bit, mono PCM in uncompressed format. Then it will work.
Upvotes: 0
Reputation:
If you are on java 7
, there are new (JavaFX
) classes there that are easier to use:
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.
Upvotes: 1
Reputation: 3820
Add mp3plugin.jar in your classpath.
http://pscode.org/lib/mp3plugin.jar
Upvotes: 2