Last active
December 12, 2019 11:06
-
-
Save FR073N/9902559 to your computer and use it in GitHub Desktop.
Android Sound Manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Constants { | |
public static final int SOUND_SELECT = R.raw.select; | |
public static final int SOUND_LOCKED = R.raw.locked; | |
public static final int SOUND_OPEN = R.raw.open; | |
public static final int SOUND_CLOSE = R.raw.close; | |
public static final void initSoundManager(Context context, SoundManager soundManager){ | |
soundManager.addSound(context, SOUND_SELECT); | |
soundManager.addSound(context, SOUND_LOCKED); | |
soundManager.addSound(context, SOUND_OPEN); | |
soundManager.addSound(context, SOUND_CLOSE); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyApplication extends Application { | |
private SoundManager soundManager; | |
public SoundManager getSoundManager() { | |
if (soundManager == null) { | |
soundManager = new SoundManager(); | |
Constants.initSoundManager(this, soundManager); | |
} | |
return soundManager; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SoundManager { | |
private SoundPool mSoundPool; | |
private SparseIntArray mSoundPoolMap = new SparseIntArray(); | |
private Handler mHandler = new Handler(); | |
private boolean mMuted = false; | |
private static final int MAX_STREAMS = 2; | |
private static final int STOP_DELAY_MILLIS = 3000; | |
public SoundManager() { | |
mSoundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0); | |
} | |
/** | |
* Put the sounds to their correspondig keys in sound pool. | |
*/ | |
public void addSound(Context context, int soundID) { | |
mSoundPoolMap.put(soundID, mSoundPool.load(context, soundID, 1)); | |
} | |
/** | |
* Find sound with the key and play it | |
*/ | |
public void playSound(int soundID) { | |
if(mMuted){ | |
return; | |
} | |
boolean hasSound = mSoundPoolMap.indexOfKey(soundID) >= 0; | |
if(!hasSound){ | |
return; | |
} | |
final int soundId = mSoundPool.play(mSoundPoolMap.get(soundID), 1, 1, 1, 0, 1f); | |
scheduleSoundStop(soundId); | |
} | |
/** | |
* Schedule the current sound to stop after set milliseconds | |
*/ | |
private void scheduleSoundStop(final int soundId){ | |
mHandler.postDelayed(new Runnable() { | |
public void run() { | |
mSoundPool.stop(soundId); | |
} | |
}, STOP_DELAY_MILLIS); | |
} | |
/** | |
* Initialize the control stream with the activity to music | |
*/ | |
public static void initStreamTypeMedia(Activity activity){ | |
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC); | |
} | |
public static int getStreamMusicLevel(Activity activity){ | |
AudioManager am = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE); | |
return am.getStreamVolume(AudioManager.STREAM_MUSIC); | |
} | |
/** | |
* Is sound muted | |
*/ | |
public void setMuted(boolean muted) { | |
this.mMuted = muted; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment