5A – FINAL TOUCHES - SOUNDS

 

 

ADDING SOUND

 

Adding sound using LibGDX and Eclipse is quite easy.  Below are the general details followed by specific details for our Pong game.

 

LIBGDX VS REGULAR JAVA

 

A LibGDX program will handle files differently than a regular Java program because it needs to be able to adapt to the type of project – desktop, ios, android or html – all of which have their own ways of working with files.

 

Of course, all of this work that LibGDX does is hidden from you.


IN GENERAL

 

The following code would create a Sound object from the file called coco.mp3:

 

    FileHandle myFile = Gdx.files.internal("coco.mp3 ");

    Sound snd = Gdx.audio.newSound(myFile);

 

We could then play the sound using:

 

    snd.play();

 

FILE LOCATION

 

Files need to be placed inside the core project’s folder and then inside the assets subfolder.

 

Note that it is also important that Eclipse is aware that the file is inside the assets folder.  This can be verified by navigating to the assets folder from inside Eclipse and checking to see if Eclipse lists the filename.  If it doesn’t, you can tell Eclipse to update its listing of files in the folder by right-clicking on the assets folder and choosing refresh.

 

 

OUR PONG GAME

 

THE FILES

 

Download these files pongBounce.mp3 & pongScore.mp3 and place them in the assets folder.

 

Note that this guide will only work with the bounce sound.  You can add the score sound later if you wish.

 

WHERE TO USE SOUNDS?

 

We will create the sound objects in PongGame and pass them to the Ball class.  Sounds will be played from inside the Ball class since they will correspond with the ball’s location.

 

PONGGAME CLASS – NEW DATA FIELDS

 

We will declare an object of type Sound.

 

   private Sound bounce;

 

PONGGAME CLASS – CREATE METHOD

 

We will now be passing this sound to the Ball class constructor.  So just before the ball is constructed, you need to create the bounce sound.  Then, we pass the sound to the Ball constructor by adding it as an argument.

 

      FileHandle myFile = Gdx.files.internal("pongBounce.mp3");

      bounce = Gdx.audio.newSound(myFile);   

      b = new Ball(250,250,0.5f,3,25,25,bounce);

 

NOTE: You now have an error in your code.  This is because your constructor for the Ball class doesn’t yet have a Sound argument.  Let’s go fix that right now.

 

BALL CLASS – DATA FIELD AND CONSTRUCTOR

 

We add a Sound object as a data field.  We also change the constructor so we can pass a sound in as a parameter.

 

Here is the new data field and the changed constructor:

 

private Sound bounce;

  

   public Ball(float x, float y, float vx, float vy, float w, float h, Sound bounce)

   {

         this.x = x;

         this.y = y;

         this.velocityX = vx;

         this.velocityY = vy;

         this.width = w;

         this.height = h;

         this.bounce = bounce;

   }

 

BALL CLASS – UPDATE METHOD

 

Now we simply use bounce.play() anytime we want the sound.  In our case, we want the sound to play whenever the ball hits the top, bottom or either paddle.

 

Place bounce.play() at the proper locations so that you hear the noise when the ball bounces off of something.