Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a soundmanager/audiomanager class, hope it helps or points you in the right direction ;)</p> <pre><code>import java.util.HashMap; import java.util.Random; import android.content.Context; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.SoundPool; public class SoundManager { public boolean audio; public boolean audioSoundFX; public boolean audioMusic; private SoundPool mSoundPool; private HashMap&lt;Integer, Integer&gt; mSoundPoolMap; private AudioManager mAudioManager; private Context mContext; private Random random = new Random(); private MediaPlayer menuMusic; private MediaPlayer gameMusic; private int menuMusicCurrentPos = 0; private int gameMusicCurrentPos = 0; public static final SoundManager INSTANCE = new SoundManager(); private SoundManager() { } public void setAudio(boolean audioOn){ if(audioOn){ audio = true; } if(!audioOn) { audio = false; } } public void setSoundFX(boolean soundFX){ if(soundFX){ audioSoundFX = true; } if(!soundFX) { audioSoundFX = false; } } public void setMusic(boolean music){ if(music){ audioMusic = true; } if(!music) { audioMusic = false; } } public void initSounds(Context theContext) { if (mSoundPool == null){ mContext = theContext; mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); mSoundPoolMap = new HashMap&lt;Integer, Integer&gt;(); mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE); mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.ufo_laser, 1)); mSoundPoolMap.put(2, mSoundPool.load(mContext, R.raw.enemy_hunter_one_laser, 1)); mSoundPoolMap.put(3, mSoundPool.load(mContext, R.raw.enemyhuntertwomissile, 1)); mSoundPoolMap.put(4, mSoundPool.load(mContext, R.raw.enemy_hunter_three_laser, 1)); mSoundPoolMap.put(5, mSoundPool.load(mContext, R.raw.enemy_drone, 1)); mSoundPoolMap.put(6, mSoundPool.load(mContext, R.raw.kamikaze, 1)); mSoundPoolMap.put(14, mSoundPool.load(mContext, R.raw.exploastroidshard, 1)); mSoundPoolMap.put(11, mSoundPool.load(mContext, R.raw.health, 1)); mSoundPoolMap.put(12, mSoundPool.load(mContext, R.raw.energy, 1)); mSoundPoolMap.put(13, mSoundPool.load(mContext, R.raw.shield, 1)); mSoundPoolMap.put(15, mSoundPool.load(mContext, R.raw.gameover, 1)); mSoundPoolMap.put(16, mSoundPool.load(mContext, R.raw.gameoverexplo, 1)); mSoundPoolMap.put(17, mSoundPool.load(mContext, R.raw.menu_beep, 1)); mSoundPoolMap.put(20, mSoundPool.load(mContext, R.raw.menu_beep, 1)); menuMusic = MediaPlayer.create(mContext, R.raw.musicmenu); } } public void playSound(int index, boolean pitching, int loop){ if(audioSoundFX == true &amp;&amp; audio == true){ float randomPitch; if (pitching){ randomPitch = (float)(random.nextInt(3) + 9) / 10; }else{ randomPitch = 1; } float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); mSoundPool.play((Integer) mSoundPoolMap.get(index), streamVolume, streamVolume, 1, loop, randomPitch); } } public void playMenuMusic(){ if(audioMusic == true &amp;&amp; audio == true){ if (menuMusic == null){ if(MediaPlayer.create(mContext, R.raw.musicmenu) != null) { menuMusic = MediaPlayer.create(mContext, R.raw.musicmenu); if(menuMusicCurrentPos != 0){ menuMusic.seekTo(menuMusicCurrentPos); } menuMusic.start(); menuMusic.setVolume(1f , 1f); menuMusic.setLooping(true); } } } } public void releaseMenuMusic(){ if(menuMusic != null){ this.menuMusicCurrentPos = menuMusic.getCurrentPosition(); menuMusic.release(); menuMusic = null; } } public void playGameMusic(){ if(audioMusic == true &amp;&amp; audio == true){ if (gameMusic == null){ gameMusic = MediaPlayer.create(mContext, R.raw.music_game); if(menuMusicCurrentPos != 0){ gameMusic.seekTo(gameMusicCurrentPos); } gameMusic.start(); gameMusic.setVolume(1f , 1f); gameMusic.setLooping(true); } } } public void releaseGameMusic(){ if(gameMusic != null){ this.gameMusicCurrentPos = gameMusic.getCurrentPosition(); gameMusic.release(); gameMusic = null; } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload