Created
January 24, 2017 20:15
-
-
Save JakeSteam/94a0b5bce8396c00eafa4a89c237e834 to your computer and use it in GitHub Desktop.
"Android: Playing Random Sounds" for GameDevAlgorithms.com
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
SoundHelper.playSound(this, SoundHelper.smithingSounds); |
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
package uk.co.jakelee.blacksmith.helper; | |
import android.content.Context; | |
import android.media.MediaPlayer; | |
import android.util.Log; | |
import java.util.Random; | |
import uk.co.jakelee.blacksmith.R; | |
import uk.co.jakelee.blacksmith.model.Setting; | |
public class SoundHelper { | |
public static final int[] enchantingSounds = {R.raw.enchant1}; | |
public static final int[] sellingSounds = {R.raw.sell1, R.raw.sell2}; | |
public static final int[] smithingSounds = {R.raw.smith1, R.raw.smith2, R.raw.smith3}; | |
public static final int[] walkingSounds = {R.raw.footsteps1}; | |
public static final int[] transitionSounds = {R.raw.slide1, R.raw.slide2, R.raw.slide3}; | |
// If an array is passed, pick one at random to play. | |
public static void playSound(Context context, int[] sounds) { | |
int soundID = sounds[new Random().nextInt(sounds.length)]; | |
playSound(context, soundID); | |
} | |
private static void playSound(Context context, int soundID) { | |
// Only play if the user has sounds enabled. | |
if (Setting.getSafeBoolean(Constants.SETTING_SOUNDS)) { | |
try { | |
MediaPlayer mediaPlayer = MediaPlayer.create(context, soundID); | |
mediaPlayer.start(); | |
} catch (Exception e) { | |
Log.d("Blacksmith", e.toString()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment