Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Easiest way of playing sound and music?  (Read 1005 times)
0 Members and 1 Guest are viewing this topic.
Offline Zeta

Junior Member


Medals: 1
Exp: 1 year



« Posted 2013-09-28 19:36:03 »

What is the Easiest way of playing sounds and music in Java(Excluding Applets)?

Thank you.

A clever person solves a problem. A wise person avoids it. -Albert Einstein
https://trello.com/zaniarm/recommend
Offline Mac70
« Reply #1 - Posted 2013-09-28 19:45:14 »

If you need something REALLY simple, you can use Java default sound library.

Otherwise, use OpenAL (for example in LWJGL).

Check out my Devblog! Smiley
Offline GlennBrann
« Reply #2 - Posted 2013-09-28 20:07:36 »

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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
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:
1  
2  
3  
4  
5  
//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.

- GlennBrann
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #3 - Posted 2013-09-28 20:21:54 »

+1 Very easy to use, nice!

Offline Zeta

Junior Member


Medals: 1
Exp: 1 year



« Reply #4 - Posted 2013-09-28 21:23:30 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
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:
1  
2  
3  
4  
5  
//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.

- GlennBrann

Thanks this was very helpful  Cool. There is one problem thought. I have a ball that bounces around the screen and it does not always play the sound when colliding.

A clever person solves a problem. A wise person avoids it. -Albert Einstein
https://trello.com/zaniarm/recommend
Offline GlennBrann
« Reply #5 - Posted 2013-09-28 21:36:43 »

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
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #6 - Posted 2013-09-28 22:23:10 »

If you can post your collision code we could help!

Offline Cero
« Reply #7 - Posted 2013-09-28 22:38:01 »

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 :D

Offline Jeremy
« Reply #8 - Posted 2013-09-28 23:34:34 »

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.

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline DrZoidberg

Senior Member


Medals: 15



« Reply #9 - Posted 2013-09-29 01:17:30 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Zeta

Junior Member


Medals: 1
Exp: 1 year



« Reply #10 - Posted 2013-09-29 08:40:45 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
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
1  
2  
3  
4  
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

A clever person solves a problem. A wise person avoids it. -Albert Einstein
https://trello.com/zaniarm/recommend
Offline SHC
« Reply #11 - Posted 2013-09-29 09:31:15 »

You can check my article Playing Sound. It is based on Java Sound and shows three ways of playing sounds.

Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #12 - Posted 2013-09-29 12:38:57 »

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.

Offline Cero
« Reply #13 - Posted 2013-09-29 23:59:01 »

I don't know why people use OpenAL etc...

The Java sound library works perfectly fine for me.

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

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (23 views)
2014-08-19 09:29:53

Tekkerue (22 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (57 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (29 views)
2014-08-08 02:01:56

Norakomi (36 views)
2014-08-06 19:49:38

BurntPizza (66 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!