exponentialFun
exponentialFun

Reputation: 225

Game Sound effects in Android

I am developing a simple game in Android. I want to add sound effects for each of the touch events. However I have add background sound effect that runs throughout the game. But how can add tiny sound effect for touching any character of the game. For better understanding following is my class design : I have a main activity from where as view I'm calling my GameView class that extends surfaceView. For the bacground sound I just created the sound at mainActivity and then called that GameView class as bellow:

public class MainActivity extends Activity {
    MediaPlayer backgroundMusic;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
         backgroundMusic = MediaPlayer.create(MainActivity.this, R.raw.bg);


         backgroundMusic.setLooping(true);
         backgroundMusic.setVolume(10.0f, 3.0f);
         backgroundMusic.start();

        setContentView(new GameView(this));
    }
}

And following is my GameView class. I want to add sound effect here in this class onTouchEvent as bellow:

public class GameView extends SurfaceView {
 @Override
 public boolean onTouchEvent(MotionEvent event) {
   //checking condition I want to give different sound here.

  }
}

I tried to do it as mainActivity (that is using MediaPlayer.creat() ), but it shows error. Anybody knows how to add such sound effect on the basis of my class design ?

Upvotes: 13

Views: 23917

Answers (3)

Filip Zymek
Filip Zymek

Reputation: 2646

For short sound effect like explosions, coin collections etc, it is better to use SoundPool.

You just need to create a sound pool :

SoundPool sp = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);

In Lollipop and later:

AudioAttributes attrs = new AudioAttributes.Builder()
        .setUsage(AudioAttributes.USAGE_GAME)
        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
        .build();
SoundPool sp = new SoundPool.Builder()
        .setMaxStreams(10)
        .setAudioAttributes(attrs)
        .build();

This creates sound pool for max. of 10 sound streams (i.e. how many simultaneous sound effects can be played at any one time) and uses AudioManager.STREAM_MUSIC as sounds stream.

Be sure to also set the volume control in your Activity, so the user is able to change the volume of the proper stream:

setVolumeControlStream(AudioManager.STREAM_MUSIC);

Than, you need to load sound effects into pool and give them their identifiers:

int soundIds[] = new int[10];
soundIds[0] = sp.load(context, R.raw.your_sound, 1);
//rest of sounds goes here

You need to pass a context to load method, so either you do this inside your activity, or get is from somwhere else.

And final step to play sound is to call play method:

sp.play(soundIds[0], 1, 1, 1, 0, 1.0);

parameters are:

  • soundID a soundID returned by the load() function

  • leftVolume left volume value (range = 0.0 to 1.0)

  • rightVolume right volume value (range = 0.0 to 1.0)

  • priority stream priority (0 = lowest priority)

  • loop loop mode (0 = no loop, -1 = loop forever)

  • rate playback rate (1.0 = normal playback, range 0.5 to 2.0)

You need to remember, that SoundPool should not use media files over 1MB, the smaller the files, the better effect and performance you have.

Be sure to release the SoundPool when you are done, or in Activity.onDestroy.

sp.release();

Hope this helps

Upvotes: 57

asiby
asiby

Reputation: 3399

Just do the following ...

MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.combo);
mp.start();

I found it at How to play a Sound Effect in Android

Upvotes: 0

meredrica
meredrica

Reputation: 2563

GameView is not a subclass of Context. Pass the Activity or the ApplicationContext to the Mediaplayer

Upvotes: 2

Related Questions