I can't hear any sound in my Android Simulator in my Mac. Some times, verily little time I can hear it. I give you my code of my Sound Manager. But i think its configuration problems
I copy the code from a page that they said it works. And i already try about 5 different codes and none works. My sound are in WAV game: 100k menu 800k
Thanks
import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager {
static private SoundManager _instance;
private static SoundPool mSoundPool;
private static HashMap<Integer, Integer> mSoundPoolMap;
private static AudioManager mAudioManager;
private static Context mContext;
private SoundManager()
{
}
/**
* Requests the instance of the Sound Manager and creates it
* if it does not exist.
*
* #return Returns the single instance of the SoundManager
*/
static synchronized public SoundManager getInstance()
{
if (_instance == null)
_instance = new SoundManager();
return _instance;
}
/**
* Initialises the storage for the sounds
*
* #param theContext The Application context
*/
public static void initSounds(Context theContext)
{
mContext = theContext;
mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
/**
* Add a new Sound to the SoundPool
*
* #param Index - The Sound Index for Retrieval
* #param SoundID - The Android ID for the Sound asset.
*/
public static void addSound(int Index,int SoundID)
{
mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}
/**
* Loads the various sound assets
* Currently hard coded but could easily be changed to be flexible.
*/
public static void loadSounds()
{
mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.menu, 1));
mSoundPoolMap.put(2, mSoundPool.load(mContext, R.raw.game, 1));
}
/**
* Plays a Sound
*
* #param index - The Index of the Sound to be played
* #param speed - The Speed to play not, not currently used but included for compatibility
*/
public static void playSound(int index,float speed)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, speed);
}
/**
* Stop a Sound
* #param index - index of the sound to be stopped
*/
public static void stopSound(int index)
{
mSoundPool.stop(mSoundPoolMap.get(index));
}
public static void cleanup()
{
mSoundPool.release();
mSoundPool = null;
mSoundPoolMap.clear();
mAudioManager.unloadSoundEffects();
_instance = null;
}
}
Basically, you have to set a flag in the config settings to tell it what kind of audio to output, and what device. It's a pain. I wrote about it here:
http://www.banane.com/2012/01/11/joy-of-android-playing-sound/
That worked on my old MacBook. Just upgraded to a Mac Air and the solution has changed, not sure, but -audio is the solution now. I'll post here when I get it working.
I SOLVED IT,
Just re-install all the eclipse and SDK. I try in another computer and works fine. The same code
Related
I use simple SoundManager for play sounds from sounpool. My problem is, sound not stop playing when I exit from app, even I use mySound.release();.
Otherwise, everything works as it should, here is my code.
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager {
private Context pContext;
private SoundPool mySound;
private float rate = 1.0f;
private float masterVolume = 1.0f;
private float leftVolume = 1.0f;
private float rightVolume = 1.0f;
private float balance = 0.5f;
// Constructor, setup the audio manager and store the app context
public SoundManager(Context appContext)
{
mySound = new SoundPool(16, AudioManager.STREAM_MUSIC, 100);
pContext = appContext;
}
// Load up a sound and return the id
public int load(int sound_id)
{
return mySound.load(pContext, sound_id, 1);
}
// Play a sound
public void play(int sound_id)
{
mySound.play(sound_id, leftVolume, rightVolume, 1, 0, rate);
}
// Set volume values based on existing balance value
public void setVolume(float vol)
{
masterVolume = vol;
if(balance < 1.0f)
{
leftVolume = masterVolume;
rightVolume = masterVolume * balance;
}
else
{
rightVolume = masterVolume;
leftVolume = masterVolume * ( 2.0f - balance );
}
}
public void unloadAll()
{
mySound.release();
}
}
The method play() return a int StreamId. You need to keep this StreamId on a variable and to call the stop method. For example:
Declare a Stream Id variable:
private int mStreamId;
Then, keep it when you start a new sound stream:
// Play a sound
public void play(int sound_id)
{
mStreamId = mySound.play(sound_id, leftVolume, rightVolume, 1, 0, rate);
}
So you can stop it doing:
mySound.stop(mStreamId);
Now you can stop your sound stream on the activities events onStop, onPause or where you want.
EDIT:
The best way to release your SoundPool resource is call release and set the reference to null after you stop playing the sound. For example:
#Override
protected void onPause() {
super.onPause();
mSoundManager.unloadAll();
}
On your SoundManager, change your unloadAll method to:
public void unloadAll()
{
mySound.release();
mySound = null;
}
Note the following:
onPause – is always called when the activity is about to go into the
background.
release: releases all the resources used by the SoundPool
object null: this SoundPool object can no longer be used (like #DanXPrado
said) so set it to null to help the Garbage Collector identify it as
reclaimable.
For more details, see this tutorial about SoundPool.
Hope this helps.
I'm currently facing an issue with my android game. Normally when calling SoundPool.play() the function needs about 0.003 seconds to finish, but sometimes it takes 0.2 seconds which makes my game stutter. where could his anomaly come from?
thanks to Tim, using a Thread for playing seemed to workaround the problem successfully.
Thread
package org.racenet.racesow.threads;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import org.racenet.racesow.models.SoundItem;
import android.media.SoundPool;
/**
* Thread for playing sounds
*
* #author soh#zolex
*
*/
public class SoundThread extends Thread {
private SoundPool soundPool;
public BlockingQueue<SoundItem> sounds = new LinkedBlockingQueue<SoundItem>();
public boolean stop = false;
/**
* Constructor
*
* #param soundPool
*/
public SoundThread(SoundPool soundPool) {
this.soundPool = soundPool;
}
/**
* Dispose a sound
*
* #param soundID
*/
public void unloadSound(int soundID) {
this.soundPool.unload(soundID);
}
#Override
/**
* Wait for sounds to play
*/
public void run() {
try {
SoundItem item;
while (!this.stop) {
item = this.sounds.take();
if (item.stop) {
this.stop = true;
break;
}
this.soundPool.play(item.soundID, item.volume, item.volume, 0, 0, 1);
}
} catch (InterruptedException e) {}
}
}
SoundItem
package org.racenet.racesow.models;
/**
* SoundItem will be passed to the SoundThread which
* will handle the playing of sounds
*
* #author soh#zolex
*
*/
public class SoundItem {
public int soundID;
public float volume;
public boolean stop = false;
/**
* Default constructor
*
* #param soundID
* #param volume
*/
public SoundItem(int soundID, float volume) {
this.soundID = soundID;
this.volume = volume;
}
/**
* Constructor for the item
* which will kill the thread
*
* #param stop
*/
public SoundItem(boolean stop) {
this.stop = stop;
}
}
I started working on AndEngine and I have learnt a lot but i am stuck in creating levels selector for my game. I know this is something to do with getTextureManager() as i went through this when i am trying to attach ITextureRegion to my sprite object, that i solved but this time i am going nowhere for last day.
mainActivity.java
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureManager;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.color.Color;
import android.graphics.Typeface;
import android.util.Log;
public class Main_Activity extends SimpleBaseGameActivity {
EngineOptions engine;
Camera mCamera;
private static final int WIDTH = 800;
private static final int HEIGHT = 480;
LevelSelector ls;
Scene mScene;
public org.andengine.opengl.font.Font mFont;
ITextureRegion mTextureRegion;
#Override
public EngineOptions onCreateEngineOptions() {
mCamera = new Camera(0, 0, WIDTH, HEIGHT);
engine = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), mCamera);
engine.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
return engine;
}
#Override
protected void onCreateResources() {
BuildableBitmapTextureAtlas mBitmapTextureAtlas = new BuildableBitmapTextureAtlas(this.getTextureManager() ,32 , 32);
mTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, this, "marble.png");
try {
mBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 1));
mBitmapTextureAtlas.load();
} catch (TextureAtlasBuilderException e) {
Log.e("", ""+e);
}
this.mFont = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32);
this.mFont.load();
}
#Override
protected Scene onCreateScene() {
mScene = new Scene();
mScene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
ls = new LevelSelector(5, 2, WIDTH, HEIGHT, mScene, mEngine);
ls.createTiles(mTextureRegion, mFont);
ls.show();
return mScene;
}
}
LayerSelector.java
import org.andengine.engine.options.EngineOptions;
import org.andengine.entity.Entity;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import android.service.wallpaper.WallpaperService.Engine;
public class LevelSelector extends Entity {
/* Level selector layer properties */
private final int COLUMNS = 6;
private final int ROWS = 6;
/* Level selector tile properties */
private final int TILE_DIMENSION = 50;
private final int TILE_PADDING = 15;
private final Scene mScene;
private final org.andengine.engine.Engine mEngine;
/*
* The mChapter variable can allow each LevelSelector object to contain
* level tiles which begin levels in different chapters.
*/
private final int mChapter;
/* Variable containing the current max level unlocked */
private final int mMaxLevel;
/* Camera width and height are needed for the layout */
private final int mCameraWidth;
private final int mCameraHeight;
/* Initial x/y coordinates used for tile positioning */
private final float mInitialX;
private final float mInitialY;
/*
* Variable which defines whether the LevelSelector is hidden or visible
*/
private boolean mHidden = true;
/**
* The LevelSelector object can be used to display a grid of level tiles for
* user selection.
*
* #param pMaxLevel
* Current max unlocked level.
* #param pChapter
* Chapter/world number of this particular LevelSelector.
* #param pCameraWidth
* Camera object's width value.
* #param pCameraHeight
* Camera object's height value.
* #param pScene
* The Scene in which the LevelSelector will be displayed on.
* #param pEngine
* AndEngine's mEngine object.
*/
public LevelSelector(final int pMaxLevel, final int pChapter,
final int pCameraWidth, final int pCameraHeight,
final Scene pScene, final org.andengine.engine.Engine pEngine) {
/* Initialize member variables */
this.mScene = pScene;
this.mEngine = pEngine;
this.mChapter = pChapter;
this.mMaxLevel = pMaxLevel;
this.mCameraWidth = pCameraWidth;
this.mCameraHeight = pCameraHeight;
/*
* Obtain the initial tile's X coordinate by subtracting half of the
* entire level selector width including all tiles and padding from the
* center of the Scene
*/
final float halfLevelSelectorWidth = ((TILE_DIMENSION * COLUMNS) + TILE_PADDING
* (COLUMNS - 1)) * 0.5f;
this.mInitialX = (this.mCameraWidth * 0.5f) - halfLevelSelectorWidth;
/* Same math as above applies to the Y coordinate */
final float halfLevelSelectorHeight = ((TILE_DIMENSION * ROWS) + TILE_PADDING
* (ROWS - 1)) * 0.5f;
this.mInitialY = (this.mCameraHeight * 0.5f) + halfLevelSelectorHeight;
mEngine.getTextureManager();
}
/**
* Create the level tiles with a customized ITextureRegion representation as
* well as a customized Font.
*
* #param pTextureRegion
* The ITextureRegion to supply each of the level tiles.
* #param pFont
* The Font to be displayed by Text written on the tiles,
* specifying tile level number for example.
*/
public void createTiles(final ITextureRegion pTextureRegion,
final org.andengine.opengl.font.Font pFont) {
/* Temp coordinates for placing level tiles */
float tempX = this.mInitialX + TILE_DIMENSION * 0.5f;
float tempY = this.mInitialY - TILE_DIMENSION * 0.5f;
/* Current level of the tile to be placed */
int currentTileLevel = 1;
/*
* Loop through the Rows, adjusting tempY coordinate after each
* iteration
*/
for (int i = 0; i < ROWS; i++) {
/*
* Loop through the column positions, placing a LevelTile in each
* column
*/
for (int o = 0; o < COLUMNS; o++) {
final boolean locked;
/* Determine whether the current tile is locked or not */
if (currentTileLevel <= mMaxLevel) {
locked = false;
} else {
locked = true;
}
/* Create a level tile */
LevelTile levelTile = new LevelTile(tempX, tempY, locked,
currentTileLevel, pTextureRegion, pFont);
/*
* Attach the level tile's text based on the locked and
* currentTileLevel variables pass to its constructor
*/
levelTile.attachText();
/* Register & Attach the levelTile object to the LevelSelector */
mScene.registerTouchArea(levelTile);
this.attachChild(levelTile);
/* Increment the tempX coordinate to the next column */
tempX = tempX + TILE_DIMENSION + TILE_PADDING;
/* Increment the level tile count */
currentTileLevel++;
}
/* Reposition the tempX coordinate back to the first row (far left) */
tempX = mInitialX + TILE_DIMENSION * 0.5f;
/* Reposition the tempY coordinate for the next row to apply tiles */
tempY = tempY - TILE_DIMENSION - TILE_PADDING;
}
}
/**
* Display the LevelSelector on the Scene.
*/
public void show() {
/* Register as non-hidden, allowing touch events */
mHidden = false;
/* Attach the LevelSelector the the Scene if it currently has no parent */
if (!this.hasParent()) {
mScene.attachChild(this);
}
/* Set the LevelSelector to visible */
this.setVisible(true);
}
/**
* Hide the LevelSelector on the Scene.
*/
public void hide() {
/* Register as hidden, disallowing touch events */
mHidden = true;
/* Remove the LevelSelector from view */
this.setVisible(false);
}
public class LevelTile extends Sprite {
/*
* The LevelTile should keep track of level number and lock status. Feel
* free to add additional data within level tiles
*/
private final boolean mIsLocked;
private final int mLevelNumber;
private final org.andengine.opengl.font.Font mFont;
private Text mTileText;
/*
* Each level tile will be sized according to the constant
* TILE_DIMENSION within the LevelSelector class
*/
public LevelTile(float pX, float pY, boolean pIsLocked,
int pLevelNumber, ITextureRegion pTextureRegion, org.andengine.opengl.font.Font pFont) {
super(pX, pY, LevelSelector.this.TILE_DIMENSION,
LevelSelector.this.TILE_DIMENSION, pTextureRegion,
LevelSelector.this.mEngine.getVertexBufferObjectManager());
/* Initialize the necessary variables for the LevelTile */
this.mFont = pFont;
this.mIsLocked = pIsLocked;
this.mLevelNumber = pLevelNumber;
}
/* Method used to obtain whether or not this level tile represents a
* level which is currently locked */
public boolean isLocked() {
return this.mIsLocked;
}
/* Method used to obtain this specific level tiles level number */
public int getLevelNumber() {
return this.mLevelNumber;
}
/*
* Attach the LevelTile's text to itself based on whether it's locked or
* not. If not, then the level number will be displayed on the level
* tile.
*/
public void attachText() {
String tileTextString = null;
/* If the tile's text is currently null... */
if (this.mTileText == null) {
/*
* Determine the tile's string based on whether it's locked or
* not
*/
if (this.mIsLocked) {
tileTextString = "Locked";
} else {
tileTextString = String.valueOf(this.mLevelNumber);
}
/* Setup the text position to be placed in the center of the tile */
final float textPositionX = LevelSelector.this.TILE_DIMENSION * 0.5f;
final float textPositionY = textPositionX;
/* Create the tile's text in the center of the tile */
this.mTileText = new Text( textPositionX,
textPositionY, this.mFont,
tileTextString, tileTextString.length(),
LevelSelector.this.mEngine
.getVertexBufferObjectManager());
/* Attach the Text to the LevelTile */
this.attachChild(mTileText);
}
}
#Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY) {
/* If the LevelSelector is not hidden, proceed to execute the touch
* event */
if (!LevelSelector.this.mHidden) {
/* If a level tile is initially pressed down on */
if (pSceneTouchEvent.isActionDown()) {
/* If this level tile is locked... */
if (this.mIsLocked) {
/* Tile Locked event... */
LevelSelector.this.mScene.getBackground().setColor(
org.andengine.util.color.Color.RED);
} else {
/* Tile unlocked event... This event would likely prompt
* level loading but without getting too complicated we
* will simply set the Scene's background color to green */
LevelSelector.this.mScene.getBackground().setColor(
org.andengine.util.color.Color.GREEN);
/**
* Example level loading:
* LevelSelector.this.hide();
* SceneManager.loadLevel(this.mLevelNumber);
*/
}
return true;
}
}
return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
pTouchAreaLocalY);
}
}
}
FYI, this code belongs to code bundle in Android game development using AndEngine cookbook.
I have edited few variables like font and engine , but i have't changed the logic or flow of program.
Now when I run this I get the below screen
Alright i solved my problem, its was the padding i was using, it should't be there.
try {
mBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 1));
mBitmapTextureAtlas.load();
} catch (TextureAtlasBuilderException e) {
Log.e("", ""+e);
}
it should be
try {
mBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
mBitmapTextureAtlas.load();
} catch (TextureAtlasBuilderException e) {
Log.e("", ""+e);
}
hai i am doing one game here i need to display 100's of images into gallery,at the same time in wich image is in view that time i need to play that image related sound, i treid using sounpool for music play.i played sound correcltly but my prblem is in gallery scroling time when i move 1 image to 2 image that time both sounds 1 image,2 image related sounds are plying that time.how to solve that problem please any one suggest me..
Gallerynew .class:
public class Gallerynew extends Activity implements OnItemSelectedListener{
public boolean Visibility=true;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SoundManager.getInstance();
SoundManager.initSounds(this);
SoundManager.playSound(1, 1);
Gallery g=(Gallery)findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setSpacing(10);
g.setOnItemSelectedListener(this);
}
public class ImageAdapter extends BaseAdapter {
private Context myContext;
private int[] myImageIds = {
R.drawable.bokstavslottet01,
R.drawable.bokstavslottet02,
R.drawable.bokstavslottet03,
R.drawable.bokstavslottet04,
R.drawable.bokstavslottet05,
R.drawable.bokstavslottet06,
R.drawable.bokstavslottet07,
R.drawable.bokstavslottet08,
R.drawable.bokstavslottet09,
R.drawable.bokstavslottet10,
R.drawable.bokstavslottet11,
R.drawable.bokstavslottet12,
R.drawable.bokstavslottet13
};
public ImageAdapter(Context c) { this.myContext = c; }
public int getCount() {
// TODO Auto-generated method stub
return this.myImageIds.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(this.myContext);
i.setImageResource(this.myImageIds[position]);
/* Image should be scaled as width/height are set. */
i.setScaleType(ImageView.ScaleType.FIT_XY);
/* Set the Width/Height of the ImageView. */
i.setLayoutParams(new Gallery.LayoutParams(700, 400));
Integer e = myImageIds.length;
Log.i("","length-------"+e);
return i;
}
/** Returns the size (0.0f to 1.0f) of the views
* depending on the 'offset' to the center. */
public float getScale(boolean focused, int offset) {
/* Formula: 1 / (2 ^ offset) */
return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
}
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if(arg2==0){
SoundManager.playSound(1, 1);
}
if(arg2==1)
{
SoundManager.playSound(2, 1);
}
if(arg2==2)
{
SoundManager.playSound(3, 1);
}
if(arg2==3)
{
SoundManager.playSound(4, 1);
}
if(arg2==4)
{
SoundManager.playSound(5, 1);
}
if(arg2==5)
{
SoundManager.playSound(6, 1);
}
if(arg2==6)
{
SoundManager.playSound(7, 1);
}
if(arg2==7)
{
SoundManager.playSound(8, 1);
}
if(arg2==8)
{
SoundManager.playSound(9, 1);
}
if(arg2==9)
{
SoundManager.playSound(10, 1);
}
if(arg2==10)
{
SoundManager.playSound(11, 1);
}
if(arg2==11)
{
SoundManager.playSound(12, 1);
}
if(arg2==12)
{
SoundManager.playSound(13, 1);
}
}
#Override
public void onDestroy()
{
super.onDestroy();
SoundManager.cleanup();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
SoundManager .class:
public class SoundManager {
static private SoundManager _instance;
private static SoundPool mSoundPool;
private static HashMap<Integer, Integer> mSoundPoolMap;
private static AudioManager mAudioManager;
private static Context mContext;
private SoundManager()
{
}
/**
* Requests the instance of the Sound Manager and creates it
* if it does not exist.
*
* #return Returns the single instance of the SoundManager
*/
static synchronized public SoundManager getInstance()
{
if (_instance == null)
_instance = new SoundManager();
return _instance;
}
/**
* Initialises the storage for the sounds
*
* #param theContext The Application context
*/
public static void initSounds(Context theContext)
{
mContext = theContext;
mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
/**
* Add a new Sound to the SoundPool
*
* #param Index - The Sound Index for Retrieval
* #param SoundID - The Android ID for the Sound asset.
*/
public static void addSound(int Index,int SoundID)
{
mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}
/**
* Loads the various sound assets
* Currently hardcoded but could easily be changed to be flexible.
*/
public static void loadSounds()
{
mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.starwars, 1));
mSoundPoolMap.put(2, mSoundPool.load(mContext, R.raw.terminator, 1));
mSoundPoolMap.put(3, mSoundPool.load(mContext, R.raw.bokstavslottet_3, 1));
mSoundPoolMap.put(4, mSoundPool.load(mContext, R.raw.bokstavslottet_4, 1));
mSoundPoolMap.put(5, mSoundPool.load(mContext, R.raw.bokstavslottet_5, 1));
mSoundPoolMap.put(6, mSoundPool.load(mContext, R.raw.bokstavslottet_6, 1));
mSoundPoolMap.put(7, mSoundPool.load(mContext, R.raw.bokstavslottet_7, 1));
mSoundPoolMap.put(8, mSoundPool.load(mContext, R.raw.bokstavslottet_8, 1));
mSoundPoolMap.put(9, mSoundPool.load(mContext, R.raw.bokstavslottet_9, 1));
mSoundPoolMap.put(10, mSoundPool.load(mContext, R.raw.bokstavslottet_10, 1));
mSoundPoolMap.put(11, mSoundPool.load(mContext, R.raw.bokstavslottet_11, 1));
mSoundPoolMap.put(12, mSoundPool.load(mContext, R.raw.bokstavslottet_12, 1));
mSoundPoolMap.put(13, mSoundPool.load(mContext, R.raw.babycooing05, 1));
}
/**
* Plays a Sound
*
* #param index - The Index of the Sound to be played
* #param speed - The Speed to play not, not currently used but included for compatibility
*/
public static void playSound(int index,float speed)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, speed);
}
/**
* Stop a Sound
* #param index - index of the sound to be stopped
*/
public static void stopSound(int index)
{
mSoundPool.stop(mSoundPoolMap.get(index));
}
public static void cleanup()
{
mSoundPool.release();
mSoundPool = null;
mSoundPoolMap.clear();
mAudioManager.unloadSoundEffects();
_instance = null;
}
}
i tried using mediaplyer its working good.but here each sound i need to set one mediaplyer object.in my app i need play some 100'of sounds.so is there any alternative solutoin please suggest me..
If you were using mediaplayer can could have easily stopped it using mp.isplaying() then do something.
But even i had the same issue i can suggest you one thing:
add a flag there and then you can check it the flag=true do something else do something
Ex: sound1 = mSoundPool.load(this, R.raw.abc, 1);
mSoundPool.play(sound1, 1, 1, 1, time - 1, 1);
loaded = true;
Now for the flag u can do like this
if(loaded == true)
{
mSoundPool.stop(sound1);
}
you can try this.
I'm currently facing an issue with my android game. Normally when calling SoundPool.play() the function needs about 0.003 seconds to finish, but sometimes it takes 0.2 seconds which makes my game stutter. where could his anomaly come from?
thanks to Tim, using a Thread for playing seemed to workaround the problem successfully.
Thread
package org.racenet.racesow.threads;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import org.racenet.racesow.models.SoundItem;
import android.media.SoundPool;
/**
* Thread for playing sounds
*
* #author soh#zolex
*
*/
public class SoundThread extends Thread {
private SoundPool soundPool;
public BlockingQueue<SoundItem> sounds = new LinkedBlockingQueue<SoundItem>();
public boolean stop = false;
/**
* Constructor
*
* #param soundPool
*/
public SoundThread(SoundPool soundPool) {
this.soundPool = soundPool;
}
/**
* Dispose a sound
*
* #param soundID
*/
public void unloadSound(int soundID) {
this.soundPool.unload(soundID);
}
#Override
/**
* Wait for sounds to play
*/
public void run() {
try {
SoundItem item;
while (!this.stop) {
item = this.sounds.take();
if (item.stop) {
this.stop = true;
break;
}
this.soundPool.play(item.soundID, item.volume, item.volume, 0, 0, 1);
}
} catch (InterruptedException e) {}
}
}
SoundItem
package org.racenet.racesow.models;
/**
* SoundItem will be passed to the SoundThread which
* will handle the playing of sounds
*
* #author soh#zolex
*
*/
public class SoundItem {
public int soundID;
public float volume;
public boolean stop = false;
/**
* Default constructor
*
* #param soundID
* #param volume
*/
public SoundItem(int soundID, float volume) {
this.soundID = soundID;
this.volume = volume;
}
/**
* Constructor for the item
* which will kill the thread
*
* #param stop
*/
public SoundItem(boolean stop) {
this.stop = stop;
}
}