DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Add Sound Effects to Your Android Game

Add Sound Effects to Your Android Game

Mark Mooibroek user avatar by
Mark Mooibroek
·
Jan. 30, 12 · Mobile Zone · Interview
Like (0)
Save
Tweet
22.76K Views

Join the DZone community and get the full member experience.

Join For Free



Every game has sound and music. In this short post I will teach you how to add sound to your own Android game. Lets start off with the base sprite example from last post.

  • Download resource: the starting code


After downloading, import the project into your workspace and build it to your device. When done correctly, you will be able to add a sprite of an explosion by touching the screen.

Now we have a working sprite example!

Next we need the sound of an explosion:

  • Download resource: explosion.wav


More code after the break:

Go to your Eclipse project and make a new folder under res named raw. Put the explosion.wav into that folder (see image).


Now let's set up the code.

Open the AnimationView.java file and add the following variables to the AnimationThread:

/** Sound variables */
private SoundPool sounds;
private int sExplosion;


Now scroll down into the constructor so we can initialize them (above the run method). Add these lines just above the closing bracket of the constructor:

sounds = new SoundPool(10, AudioManager.STREAM_MUSIC,0);
sExplosion = sounds.load(context, R.raw.explosion, 1);


Now the SoundPool is set up :) and can play 10 streams at a time. Now we just need to tell the SoundPool when to start playing, so scroll down again and find the following inside the doTouch event:

a.setXPos((int)x);
a.setYPos((int)y);


add these lines below:

sounds.play(sExplosion, 1.0f, 1.0f, 0, 0, 1.5f);


Thats it! Just run and add sprites by touching the screen. We now have a sound effect! ( Be sure to enable your sound :) )

Here is a dump of the finished Eclipse project:

  • Download GameBase with sound



Source: http://p-xr.com/android-tutorial-add-sound-effects-to-your-game/

 

Android (robot)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 3 Best Tools to Implement Kubernetes Observability
  • Refactor Switch to a One-Liner
  • 12 Modern CSS Techniques For Older CSS Problems
  • MongoDB vs. DynamoDB Head-to-Head: Which Should You Choose?

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo