DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Add Sound Effects to Your Android Game

Add Sound Effects to Your Android Game

Mark Mooibroek user avatar by
Mark Mooibroek
·
Jan. 30, 12 · Interview
Like (0)
Save
Tweet
Share
22.93K 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

  • Spring Cloud
  • Apache Kafka Is NOT Real Real-Time Data Streaming!
  • Cucumber.js Tutorial With Examples For Selenium JavaScript
  • Practical Example of Using CSS Layer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: