Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Game Sound effects in Android

Tags:

android

audio

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 ?

like image 887
exponentialFun Avatar asked Oct 09 '13 12:10

exponentialFun


People also ask

What is SFX sound in games?

Sound Effects (SFX) – all the sounds that objects in your game make (ambient, shots, hits, splashes, menu sounds, etc).

What is SFX sound effects?

Special effects (usually visual), illusions used in film, television, and entertainment. Sound effects, sounds that are artificially created or enhanced.


1 Answers

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

like image 164
Filip Zymek Avatar answered Oct 09 '22 13:10

Filip Zymek