What is the Easiest way of playing sounds and music in Java(Excluding Applets)?
Thank you.
What is the Easiest way of playing sounds and music in Java(Excluding Applets)?
Thank you.
If you need something REALLY simple, you can use Java default sound library.
Otherwise, use OpenAL (for example in LWJGL).
Well,
For quick relatively short sounds i use Clips and for Music i would either use EasyOGG(Sound Library made by kevglass) or Just regular applet sound loading/playing. The problem with applet sounds is that it doesn’t read .ogg files without making your own sound engine. So easyogg is the best for music and really long sounds.
For clips here is some sample code showing you how:
first start off with a sound class holding all your sounds.
public class Sounds {
public static Clip shooting= loadClip("/sound/shooting.wav");
private static Clip loadClip(String path){
try{
AudioInputStream sounds = AudioSystem.getAudioInputStream(Sounds.class.getResource(path));
DataLine.Info info = new DataLine.Info(Clip.class, sounds.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(sounds);
return clip;
}catch(Exception e){
e.printStackTrace();
System.exit(1);
return null;
}
}
public static void play(Clip clip){
if(clip != null){
try{
clip.stop();
clip.setFramePosition(0);
clip.start();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void stop(Clip clip){
if(clip != null){
clip.stop();
}
}
}
Then to play your sound with this basically use something like this:
//some thing like this
if(player.isShooting){
Sounds.play(Sounds.shooting);
}
Something like this. i hope this helps with your short sounds.
This is the way i do it anyway.
+1 Very easy to use, nice!
Thanks this was very helpful 8). There is one problem thought. I have a ball that bounces around the screen and it does not always play the sound when colliding.
Well one thing I can’t think of is make sure that your bounce sound doesn’t have any silence at the end of the sound make sure it’s just the bounce. Also if the ball is bouncing too fast it might cause a problem because basically the first sound bounce hasn’t finished yet so that’s why the next one won’t play. I’m not to sure how to over lap the sounds
-GlennBrann
If you can post your collision code we could help!
EasyOGG is quite easy
you may as well import the open-al jar of libgdx by itself (I think thats works) and use their sounds
but yeah easyogg is good. definitely easier and 999x better than JavaSound
I don’t know why people use OpenAL etc…
The Java sound library works perfectly fine for me. It probably uses OpenAL to some extend in the backend. Most games don’t need all that much when it comes to sound (unless you want i.e 3D sound etc)
There are various SPIs to add audio to Java’s sound library, and they work without touching anything else in the code. You could have an entire audio subsystem working over Java Sound. When it comes time to implement a new audio format (i.e, adding support for Ogg) you literally do nothing other than add that SPI to your project.
Literally Zero code was changed when I added Ogg support to JevaEngine, same with MIDI, I just found some open-sourced SPIs for Java Sound that support Ogg.
There’s no point to use anything like EasyOgg too, there are Ogg libraries that integrate seamlessly into Java’s audio library.
I changed your Sounds class. Now it should always play. However if you call play while the same clip is still playing it will be stopped and restarted. If you want an overlap i.e. the same clip playing twice at the same time, you need to create two clips from the same audio file.
I may have used more locking here than necessary but it seems to be working fine.
import javax.sound.sampled.*;
import java.util.Hashtable;
public class Sounds {
final static Hashtable<Clip, Boolean> isPlaying = new Hashtable<>();
final static Hashtable<Clip, Object> locks = new Hashtable<>();
public static Clip shooting= loadClip("/sound/shooting.wav");
private static Clip loadClip(String path){
try{
AudioInputStream sounds = AudioSystem.getAudioInputStream(Sounds.class.getResource(path));
final Clip clip = AudioSystem.getClip();
isPlaying.put(clip, false);
locks.put(clip, new Object());
clip.open(sounds);
clip.addLineListener(new LineListener() {
public void update(LineEvent e) {
LineEvent.Type type = e.getType();
if(type == type.START) {
synchronized(locks.get(clip)) {
isPlaying.put(clip, false);
}
} else if(type == type.STOP) {
synchronized(locks.get(clip)) {
if(isPlaying.get(clip)) {
clip.setFramePosition(0);
clip.start();
}
}
}
}
});
return clip;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public static void play(Clip clip){
if(clip != null){
try{
synchronized(locks.get(clip)) {
isPlaying.put(clip, true);
clip.stop();
clip.setFramePosition(0);
clip.start();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void stop(Clip clip){
if(clip != null){
clip.stop();
}
}
}
edit:
There is a much easier way to do it. Just create a new Clip every time you play a sound. That way you also get overlapping sound. You can replace the entire Sounds class with just this one method.
public static void play(String path) {
try{
AudioInputStream sounds = AudioSystem.getAudioInputStream(Sounds.class.getResource(path));
final Clip clip = AudioSystem.getClip();
clip.addLineListener(new LineListener() {
public void update(LineEvent e) {
LineEvent.Type type = e.getType();
if(type == type.STOP) clip.close();
}
});
clip.open(sounds);
clip.start();
} catch(Exception e){
e.printStackTrace();
}
}
And then just call it with the name of your sound file.
Thank you very much. I have another problem that i cant get fixed.
I still have problem with player movement and dont want to start a whole new topic for it. Here is the code for player movement.
Keylistener
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_A){
player.setVelX(-5);
e.consume();
}else if(e.getKeyCode() == KeyEvent.VK_D){
player.setVelX(5);
e.consume();
}
public void keyReleased(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_A){
player.setVelX(0);
}else if(e.getKeyCode() == KeyEvent.VK_D){
player.setVelX(0);
}
}
});
And here is the Player class movement method
public void movePlayer(){
lastPosX = x;
x += (vX * speed);
}
When you move the player to the right and after that quickly to the left it waits a moment before moving.
Here is link for the game so you can try it out for yourselves. http://www.mediafire.com/?dgowmmd1s2p586k
You can check my article Playing Sound. It is based on Java Sound and shows three ways of playing sounds.
Does it slow down and then start moving to the left? Or does it completely stop and then slowly start moving to the left? Your code should be making the player sort of “drift” when you release the key, so that may be your issue because to go left, you have to at least break even on the speed to the right to move left, if that make sense.
Without rage, are you freaking kidding me ?
The fact that JavaSound is the single worst thing the jdk has to offer has been firmly established for years. You come to java GAME DEV forum and say its not bad ?
You got balls.
Also 3D sound is not special, if you want basic stereo effect in a 2D game, you already need this, which is very powerful and easy with OpenAL