I am trying to play my soundpool methods in several classes of my android project, however they are not activity classes and so I need to use context. Whenever I write the code and start the app it crashes everytime. I muustnt be using context properly. ANY HELP WOULD BE GREATLY APPRECIATED. I will post code here.
public class Sound1{
static SoundPool soundPool;
static int soundId;
static AudioManager audioManager;
static int[] sm;
// private final Context mContext;
Context context;
public Sound1(Context context){
this.context = context;
// mContext = context.getApplicationContext();
int maxStreams = 4;
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
soundPool = new SoundPool.Builder()
.setMaxStreams(maxStreams)
.build();
} else {
soundPool = new SoundPool(maxStreams, AudioManager.STREAM_MUSIC, 0);
}
sm = new int[3];
// fill your sounds
sm[0] = soundPool.load(context, R.raw.sound1, 1);
sm[1] = soundPool.load(context, R.raw.sound1, 1);
sm[2] = soundPool.load(context, R.raw.sound1, 1);
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
}
public void playSound(int sound) {
soundPool.play(sm[sound], 1, 1, 1, 0, 1f);
}
}
Sound1 sound1 = new Sound1(this);
public Player(GameScreen gameScreen, GameEventHandler mGameEventHandler) {
// Store the centre of the screen
//screenCentre.x = gameScreen.getGame().getScreenWidth() / 2;
//screenCentre.y = gameScreen.getGame().getScreenHeight() / 2;
this.mGameEventHandler = mGameEventHandler;
//mDeck = new Deck(mGameScreen);
Move1BoundingBox = new BoundingBox(
mGameScreen.getGame().getScreenWidth()/2, mGameScreen.getGame().getScreenHeight()/2 + 20,
210, 90);
Move2BoundingBox = new BoundingBox(
mGameScreen.getGame().getScreenWidth()/2, mGameScreen.getGame().getScreenHeight()/2 + 110,
210, 90);
ExpandedBoundingBox = new BoundingBox(mGameScreen.getGame().getScreenWidth()/2,
mGameScreen.getGame().getScreenHeight()/2, 210, 150);
}
public void update(ElapsedTime elapsedTime) {
// Consider any touch events occurring since the update;
Input input = mGameScreen.getGame().getInput();
sound1.playSound(1);
if (input.getTouchEvents().size() != 0) {
if (input.existsTouch(0)) {
// Get the primary touch event
isPlayerTouch = true;
STACK TRACE
04-21 14:08:08.609 18358-18358/uk.ac.qub.eeecs.gage D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN 04-21 14:08:08.649 18358-18398/uk.ac.qub.eeecs.gage V/MediaPlayer-JNI: isPlaying: 0 04-21 14:08:08.699 18358-18398/uk.ac.qub.eeecs.gage V/MediaPlayer-JNI: isPlaying: 0 04-21 14:08:08.749 18358-18398/uk.ac.qub.eeecs.gage V/MediaPlayer-JNI: isPlaying: 0 04-21 14:08:08.969 18358-18398/uk.ac.qub.eeecs.gage E/AndroidRuntime: FATAL EXCEPTION: Thread-2376 Process: uk.ac.qub.eeecs.gage, PID: 18358 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at android.os.Handler.(Handler.java:200) at android.os.Handler.(Handler.java:114) at android.app.Activity.(Activity.java:889) at uk.ac.qub.eeecs.game.cardDemo.Player.(Player.java:0) at uk.ac.qub.eeecs.game.cardDemo.CardDemoGameScreen.(CardDemoGameScreen.java:127) at uk.ac.qub.eeecs.game.MenuScreen.update(MenuScreen.java:103) at uk.ac.qub.eeecs.gage.Game.doUpdate(Game.java:330) at uk.ac.qub.eeecs.gage.Game.access$200(Game.java:26) at uk.ac.qub.eeecs.gage.Game$GameLoop.run(Game.java:527) at java.lang.Thread.run(Thread.java:818)