Code example for SoundPool

Methods: playstop

0
	} 
	 
	public void initialEffect() 
	{ 
		this.mSoundIdStreamIdMap = new HashMap<Integer,Integer>();
		mSoundPool = new SoundPool(MAX_SIMULTANEOUS_STREAMS_DEFAULT, AudioManager.STREAM_MUSIC, SOUND_QUALITY);
		mPathSoundIDMap = new HashMap<String,Integer>();
		 
		this.mLeftVolume = 0.5f;
		this.mRightVolume = 0.5f;
		Log.i("cloudbox-app", "initialEffect succeed.");
	} 
	 
	public int createSoundIdFromAsset(String fileName){
		int soundId = INVALID_SOUND_ID;
		 
		try { 
			soundId = mSoundPool.load(mContext.getAssets().openFd(fileName), 0);
		} catch(Exception e){
			 Log.e(TAG, "error: " + e.getMessage(), e);
		} 
		 
		return soundId;
	} 
	 
    // using wave file. 
	public void loadEffect(String fileName)
    { 
		if(fileName.length() == 0)
			return; 
		if(mSoundIdStreamIdMap == null)
		{ 
			initialEffect(); 
		} 
		int soundId = INVALID_SOUND_ID;
		 
		// if the sound is preloaded, pass it 
		if (this.mPathSoundIDMap.get(fileName) == null){
			soundId = createSoundIdFromAsset(fileName);
			 
			if (soundId != INVALID_SOUND_ID){
				// the sound is loaded but has not been played 
				this.mSoundIdStreamIdMap.put(soundId, INVALID_STREAM_ID);
				 
				// record path and sound id map 
				this.mPathSoundIDMap.put(fileName, soundId);
			} 
		} 
    } 
	 
	public void releaseAllEffect() 
    { 
		if (mSoundPool != null){
			mSoundPool.release();
			mSoundPool = null;
		} 
		if (mPathSoundIDMap != null){
			mPathSoundIDMap.clear();
			mPathSoundIDMap = null;
		} 
		if (mSoundIdStreamIdMap != null){
			mSoundIdStreamIdMap.clear();
			mSoundIdStreamIdMap = null;
		} 
    } 
	public void playEffect(String fileName)
    { 
		if(fileName == null)
			return; 
		if(mPathSoundIDMap == null)
		{ 
			Log.i("cloudbox-app", "mPathSoundIDMap is null.");
			return; 
		} 
		Integer soundId = this.mPathSoundIDMap.get(fileName);
		 
		if (soundId != null){
			// the sound is preloaded, stop it first 
			 
			this.mSoundPool.stop(soundId);
			 
			// play sound 
			int streamId = this.mSoundPool.play(soundId.intValue(), this.mLeftVolume, 
					this.mRightVolume, SOUND_PRIORITY, 0, SOUND_RATE);
			 
			// record sound id and stream id map 
			this.mSoundIdStreamIdMap.put(soundId, streamId);
		} 
Stop searching for code, let great code find you!  Add Codota to your java IDE