/NightOfTheLivingGoat/app/src/main/java/com/threeLeggedGoat/nightOfTheLivingGoat/GameSoundPlayer.java

https://gitlab.com/smcdermott25/NightOfTheLivingGoat · Java · 62 lines · 44 code · 12 blank · 6 comment · 1 complexity · e8d1ab2efca0cb42e26513b17c7e9114 MD5 · raw file

  1. package com.threeLeggedGoat.nightOfTheLivingGoat;
  2. import android.content.Context;
  3. import android.media.AudioManager;
  4. import android.media.SoundPool;
  5. import android.os.SystemClock;
  6. /**
  7. * Created by arase on 29/02/2016.
  8. */
  9. public class GameSoundPlayer extends SoundPlayer {
  10. private SoundPool sp;
  11. private final int maxStreams = 15;
  12. private int background,shot,steps,kill;
  13. private float lastFootstep;
  14. private Context context;
  15. public GameSoundPlayer(Context context){
  16. super(context);
  17. this.context = context;
  18. lastFootstep = 0;
  19. resume();
  20. }
  21. public void shoot(){sp.play(shot, getCurrentVolume(), getCurrentVolume(), 1, 0, 1);}
  22. public void walk(){
  23. //timer ensures the sound isn't continuously played
  24. if (SystemClock.uptimeMillis() - lastFootstep >= 288){
  25. sp.play(steps,getCurrentVolume()/2,getCurrentVolume()/2,2,0,1);
  26. lastFootstep = SystemClock.uptimeMillis();
  27. }
  28. }
  29. public void kill(){
  30. sp.play(kill,getCurrentVolume()/2,getCurrentVolume()/2,2,0,1);
  31. }
  32. public void stop(){
  33. sp.release();
  34. }
  35. //initializes all sounds again after game was paused/first created
  36. public void resume() {
  37. sp = new SoundPool(maxStreams, AudioManager.STREAM_MUSIC,0);
  38. background = sp.load(context,R.raw.background,1);
  39. shot = sp.load(context,R.raw.shot,2);
  40. steps = sp.load(context,R.raw.foot,2);
  41. kill = sp.load(context,R.raw.boom,2);
  42. //starts playing background music once all sounds loaded
  43. sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
  44. @Override
  45. public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
  46. sp.play(background,getCurrentVolume(),getCurrentVolume(),5,-1,1f);
  47. }
  48. });
  49. }
  50. }