This is my code
public static void main(String[] args) {
try{
// open the sound file as a Java input stream
InputStream in = new FileInputStream("Sound.wav");
// create an audiostream from the inputstream
AudioStream audioStream = new AudioStream(in);
// play the audio clip with the audioplayer class
AudioPlayer.player.start(audioStream);
}
catch(Exception e){
System.out.println("Audio not found");
}
}
Sun Audio is Undocumented
You should not be using Sun Audio in your program. See here and here.
The Answer Depends on Your Library
If you are using Java Clip (built-in), try the example here. This is probably the simplest method.
If you switch to use JavaFX, you can see the API here, which has ways to loop. However, JavaFX requires a bit of package restructuring, and there is a learning curve. Unless you need JavaFX for other reasons, I would avoid it.
If you are using any other library, please consult the API for that library, or share with us what you are attempting to use. I have personally used BasicPlayer, but it is quite old.
Related
I'm trying to make an application where when I click a JButton, it plays a song. I've already figured out how to specify JButtons. However, I can't seem to find a way to play sound. I'm not going to use sun.audio, so many of the threads that I looked at didnt work. I found many low quality answers. Eventually, I settled on this code.
public static synchronized void playSound(final String url) {
new Thread(new Runnable() {
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(
Main.class.getResourceAsStream("/path/to/sounds/" + url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}
Inside my src folder, I have a folder called "Songs". I changed the file path to this:
/Songs/
Then, I tried calling the function using a wav file:
playSound("song.wav");
And then I get a null error. I believe that this is because it does not recognize the file path. The answer to my problem is most likely very obvious, but somehow I cant find it.
put song.wav in the same folder of your Main class, then the getResourceAsStream will find it.
if you're using eclipse, it can automatically copy the .wav file to the output folder.
You might want to create a class on some different java package so your entry point does not get polluted by assets.
This reading might help you: http://www.thinkplexx.com/learn/howto/java/system/java-resource-loading-explained-absolute-and-relative-names-difference-between-classloader-and-class-resource-loading
I wrote a media player, that I exported as a jar file.
It works great on my linux system. Compiled with 1.8, but to work with 1.6 as well.
Now my friend who needs to use it runs the jar on her mac (java 1.6) and the program works, does not throw an exception, but does not play sound.
Any ideas what could have gone wrong?
public void mediaPlayer()
throws Exception {
// open the sound file as a Java input stream
String soundFile = "./data/1.wav";
InputStream in = new FileInputStream(soundFile);
//
// // create an audiostream from the inputstream
AudioStream audioStream = new AudioStream(in);
// // play the audio clip with the audioplayer class
AudioPlayer.player.start(audioStream);
}
Oh, the data folder was misplaced. Solved.
This question already has answers here:
Playing .mp3 and .wav in Java?
(15 answers)
Closed 8 years ago.
I want to play an audio file when certain conditions are met but I can't figure it out. I am inexperienced in java and programming in general and copied several blocks of code and none of them work.
Trying to play this:
File type: .mp3
Location: C:\Users\Public\Music\Sample Music
I wasted close to two hours on this. Plz help with code that just plays audio from a file. Thank you.
Update: I keep getting this error
Multiple markers at this line
- Access restriction: The type AudioStream is not accessible due to restriction on required library C:\Program Files (x86)\Java\jre1.8.0_25\lib\rt.jar
- Access restriction: The type AudioStream is not accessible due to restriction on required library C:\Program Files (x86)\Java\jre1.8.0_25\lib\rt.jar
- Access restriction: The constructor AudioStream(InputStream) is not accessible due to restriction on required library C:\Program Files (x86)\Java
Check Xuggle or jLayer.
Google them and you will come up with nice results.
Good >> http://kxhitiz.blogspot.com.tr/2010/09/playing-mp3-in-java-programming.html
Java Jlayer Mp3 Player - how to repeat and stop song
public class JavaAudioPlaySoundExample
{
public static void main(String[] args)
throws Exception
{
// open the sound file as a Java input stream
String gongFile = "/Users/al/DevDaily/Projects/MeditationApp/resources/gong.au";
InputStream in = new FileInputStream(gongFile);
// create an audiostream from the inputstream
AudioStream audioStream = new AudioStream(in);
// play the audio clip with the audioplayer class
AudioPlayer.player.start(audioStream);
}
}
Hye there. I just want to play an mp3 file in my Java J2SE Desktop app. all is going fine with no exceptions but the problem is the audio sound is not heard I have checked my speakers as well use all kind a Java mp3 plugins like JMF and JLayer by JavaZoom. My code is listed below and i have added Jlayer.jar to my project also. Please help me give me solution of the problem because I'm trying since last 3 days but failed to do so! Please help me thanks in advance...
private void playMe(){
try{
String file="E://Net Beans Work Space//mp3//a.mp3";
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new Player(bis);
}catch(Exception e){
System.out.print("ERROR "+e);
}
}
Did you try to start the player?
player.start();
i am trying to loop an audio file but when i've tried the other various methods available, my music doesn't play.
My basic code is:
import java.io.*;
import sun.audio.*;
public class PlayMusic {
public void playSound() {
try {
AudioPlayer p = AudioPlayer.player;
AudioStream as = new AudioStream(new FileInputStream("02 River Flows In You.wav"));
p.start(as);
} catch (IOException IOE) {
}
}
I've never seen sun.audio.AudioPlayer in use before! I've always used the javax.sound.sampled library.
Maybe you can tell me how you came across it. It looks to me like rather old code (10 years?) and also a bit on the limited side.
I think, if you aren't getting a single playback, you should try the URL form:
AudioStream audiostream = new AudioStream(url.openStream());
AudioPlayer.player.start(audiostream);
Then try putting the code in a loop? A lot depends on whether the .start command blocks or not. If it launches a separate thread (most likely), then you will have to figure out when the file ends, and I don't see any commands provided to do that! The spec also lists the possibility of creating an ContinuousAudioDataStream and managing that.
Unless there is something I don't know about this library that I should, I recommend using either a javax.sound.sampled.Clip, or javax.sound.sampled.SourceDataLine for playback. The Clip has a boolean you can set for looping, but requires the entire file be loaded into memory first. The SourceDataLine plays back from a url or file location, and can implement a listener to tell you when the file is done.
http://docs.oracle.com/javase/tutorial/sound/playing.html