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.
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.
|
|
|
|
|