Morphis1792
Morphis1792

Reputation: 3

Sound effects Android

I want to add sound effects for each of my player events like on death. Should I use soundpool or meidaplayer and how would I go about it? For better understanding following is my class design, I have a main activity class "game" from where as view I'm calling my "GamePanel" class that extends surfaceView and draws my whole game. Any help would be greatly appreciated! Game Class below.

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class Game extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    View v1 = (new GamePanel(this));
    setContentView(v1);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_game, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);


}

}

and my GamePanel where I would like to have the sound played

   player.resetDY();
        if (!reset) {
            newGameCreated = false;
            startReset = System.nanoTime();
            reset = true;
            dissapear = true;
            explosion = new 
            Explosion(BitmapFactory.decodeResource(getResources(), R.drawable.explosion), player.getX(),
                    player.getY() - 30, 100, 100, 25);
             ///here 

Upvotes: 0

Views: 1561

Answers (1)

Michael
Michael

Reputation: 596

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

Check full answer here: source

Upvotes: 1

Related Questions