While audible good effects together with music won't necessarily brand whatever video game to a greater extent than fun, they definitely practise acquire inwards to a greater extent than
interesting together with to a greater extent than
immersive. Before I added whatever sounds to my novel Android game, I didn't actually notice they were missing because I was together with therefore focused on gameplay elements. But subsequently adding them, whenever I muted my device (or disabled the good inwards code for functioning troubleshooting) it was obvious...
something was missing.
If yous desire to acquire how to code good for your Android app or game yourself, the Android developer site's
Managing Audio Playback together with
Media Playback pages are a expert house to start. There are besides about expert sites/blogs out in that location that bring upward how to play a unmarried good upshot or play a unmarried music file, but I had problem finding a consummate solution that handled good effects AND music, together with therefore I created an good shape that allows yous to easily play music together with good effects from whatever activity. I promise this volition assistance others wanting to acquire started amongst good inwards their Android apps/games.
My GameAudio.java file together with its code are licensed nether the
Creative Commons Attribution License, pregnant yous may freely utilization it together with adjust it to your needs equally long equally yous credit me equally the master copy writer inwards a header comment. If yous wouldn't heed posting a comment hither telling me how you're using it, that would besides live actually cool together with live a expert incentive for me to portion additional code inwards the time to come :)
Download GameAudio.java equally a Zip File View Source of GameAudio.java inwards a Pop-Up Window import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.SoundPool; /* * This good shape was originally developed past times Jeromie L. Walters * together with distributed for world utilization on xx Oct 2013. This file together with all * contents are licensed nether the Creative Commons Attribution "CC BY" * license described here: http://creativecommons.org/licenses/by/3.0/ */ world shape GameAudio { someone static lastly int SOUND_STREAM_COUNT = 16; // Max let on of streams played right away someone static lastly int SOUND_QUALITY = 0; // Not used currently but laid upward to 0 (per docs) for time to come compatibility someone static lastly int SOUND_PRIORITY = 1; // Not utilization currently, but laid upward to 1 (per docs) for time to come compatibility someone static boolean mIsInitialized; someone static Context mContext; someone static AudioManager mAudioManager; someone static MediaPlayer mMediaPlayer; someone static SoundPool mSoundPool; someone static List<Integer> mSoundStreams; someone static HashMap<Integer, Integer> mSoundMap; // Initialize should live called i time inwards each activity's onCreate method if good is to live played inwards the Activity world static void initialize(Activity activity, Integer soundArrayResourceId) { mContext = activity; mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE); activity.setVolumeControlStream(AudioManager.STREAM_MUSIC); // On API Level 8, it would live proper to asking good focus from mAudioManager, // equally good equally treatment loss of good focus together with abandoning good focus when done. if (soundArrayResourceId != null) { mSoundPool = novel SoundPool(SOUND_STREAM_COUNT, AudioManager.STREAM_MUSIC, SOUND_QUALITY); mSoundStreams = novel ArrayList<Integer>(); mSoundMap = novel HashMap<Integer, Integer>(); TypedArray soundResources = mContext.getResources().obtainTypedArray(soundArrayResourceId); int soundCount = soundResources.length(); for (int i = 0; i < soundCount; i++) { int soundResourceId = soundResources.getResourceId(i, -1); if (soundResourceId != -1) { mSoundMap.put(soundResourceId, mSoundPool.load(mContext, soundResourceId, SOUND_PRIORITY)); } } soundResources.recycle(); } mIsInitialized = true; } world static void playMusic(int musicResourceId, boolean loop, float volumeMultiplier) { if (!mIsInitialized) return; // Only play i music file at a fourth dimension stopMusic(); float actualVolume = (float)mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); float maxVolume = (float)mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); float book = (actualVolume / maxVolume) * volumeMultiplier; mMediaPlayer = MediaPlayer.create(mContext, musicResourceId); mMediaPlayer.setLooping(loop); mMediaPlayer.setVolume(volume, volume); mMediaPlayer.start(); } world static void stopMusic() { if (mMediaPlayer != null) { if (mMediaPlayer.isPlaying()) { mMediaPlayer.stop(); } mMediaPlayer.release(); mMediaPlayer = null; } } world static void playSound(int soundResourceId, boolean loop) { if (!mIsInitialized) return; float charge per unit of measurement = 1.0f; int priority = 0; int loopCount = (loop ? -1 : 0); float actualVolume = (float)mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); float maxVolume = (float)mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); float book = actualVolume / maxVolume; if (mSoundPool != naught && mSoundMap != naught && mSoundMap.containsKey(soundResourceId)) { int streamId = mSoundPool.play(mSoundMap.get(soundResourceId), volume, volume, priority, loopCount, rate); mSoundStreams.add(streamId); } } world static void playSound(int soundResourceId) { playSound(soundResourceId, false); } world static void stopSounds() { if (mSoundStreams != null) { int streamCount = mSoundStreams.size(); for (int i = 0; i < streamCount; i++) { int streamId = mSoundStreams.get(i); mSoundPool.stop(streamId); } mSoundStreams.clear(); } } world static void stopAll() { stopMusic(); stopSounds(); } world static void releaseAll() { stopMusic(); if (mAudioManager != null) { mAudioManager = null; } if (mSoundPool != null) { mSoundPool.release(); mSoundPool = null; } if (mSoundStreams != null) { mSoundStreams.clear(); mSoundStreams = null; } // CRITICAL to release context to avoid Activity retentiveness leak! mContext = null; mSoundMap = null; mIsInitialized = false; } }
Below is an Activity code snippet showing how to utilization the GameAudio class. Note the placement of the
initialize
together with
releaseAll
calls inwards onResume together with onPause, respectively. The
playMusic
together with
playSound
methods tin live called anytime subsequently
intialize
together with earlier
releaseAll
.
... @Override protected void onResume() { super.onResume(); GameAudio.initialize(this, R.array.MenuSounds); GameAudio.playMusic(R.raw.music_title_screen, true, 1.0f); } @Override protected void onPause() { super.onPause(); GameAudio.releaseAll(); } world void onClick(View v) { GameAudio.playSound(R.raw.sound_button_press); ...
One lastly note, yous may conduct keep noticed the initialize method takes an array resources input parameter --
R.array.MenuSounds
inwards the snippet above. This is done to allow the GameAudio shape to practise a mapping betwixt good upshot resources identifiers similar
R.raw.sound_button_press
together with the conduct keep for the loaded good created past times the SoundPool.load function. If yous alone desire to play music, yous tin travel past times naught for that array resources identifier. Otherwise, practise an entry similar the i below inwards your res/values/arrays.xml file.
- @raw/sound_button_press
... ...
So that's it, the consummate good solution I'm using inwards my upcoming Android game. I promise this helps someone else only similar the community's awesome docs, blogs, together with websites conduct keep helped me.
0 Response to "A Consummate Android Game Well Solution"
Post a Comment