Coops5575
Coops5575

Reputation: 441

How to call a separate class to play sound?

I am currently learning game development online. I would like to add sound fx to make the game a little more fun. The program layout consists of MainActivity() and 3 threads: GameView(), GameThread() and TheGame(). TheGame() is the loop for all the updates. I have created a new class for playing sounds:

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundFX {


public static final int S1 = R.raw.ball;
public static final int S2 = R.raw.balloon;

private static SoundPool mSoundPool;
private static HashMap mSoundMap;

public static void initSounds(Context context){


    mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 100);
    mSoundMap = new HashMap(2);    // 2 sounds in sound map

    //Put sounds into SoundMap
    mSoundMap.put( S1, mSoundPool.load(context, R.raw.ball, 1) );
    mSoundMap.put( S2, mSoundPool.load(context, R.raw.balloon, 2) );


}//Initiate Sounds

public static void playSound(Context context, int soundID) {
    //No sounds yet loaded..... then Load
    if(mSoundPool == null || mSoundMap == null){

    initSounds(context);

    }

    float volume = 1 ;    // whatever in the range = 0.0 to 1.0

    // play sound with same right and left volume, with a priority of 1,
    // zero repeats (i.e play once), and a playback rate of 1f

    mSoundPool.play(soundID, volume, volume, 1, 0, 1f);

    }//Play


}//SoundFX

But when I detect a collision in TheGame loop, I would like to call the class and play the sound.

I have tried using:

SoundFX.playSound(S1);

But Eclipse is saying The Method playSound(Context, int) is not applicable for the argument (int)

Can anyone please help?

Upvotes: 0

Views: 330

Answers (1)

Saj
Saj

Reputation: 18712

You are not passing the context.

SoundFX.playSound(this, S1); 

Upvotes: 1

Related Questions