Code example for SoundPool

Methods: loadplaystop

0
		Integer soundId = this.mPathSoundIDMap.get(path);
		 
		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, isLoop ? -1 : 0, SOUND_RATE);
			 
			// record sound id and stream id map 
			this.mSoundIdStreamIdMap.put(soundId, streamId);
		} else { 
			// the effect is not prepared 
			soundId = preloadEffect(path);	
			if (soundId == INVALID_SOUND_ID){
				// can not preload effect 
				return INVALID_SOUND_ID;
			} 
			 
			/* 
			 * Someone reports that, it can not play effect for the 
			 * first time. If you are lucky to meet it. There are two 
			 * ways to resolve it. 
			 * 1. Add some delay here. I don't know how long it is, so 
			 *    I don't add it here. 
			 * 2. If you use 2.2(API level 8), you can call  
			 *    SoundPool.setOnLoadCompleteListener() to play the effect. 
			 *    Because the method is supported from 2.2, so I can't use 
			 *    it here. 
			 */ 
			playEffect(path, isLoop);
		} 
		 
		return soundId.intValue();
	} 
	 
	public void stopEffect(int soundId){
        Integer streamId = this.mSoundIdStreamIdMap.get(soundId);
         
        if (streamId != null && streamId.intValue() != INVALID_STREAM_ID){
        	this.mSoundPool.stop(streamId.intValue());
        	this.mPathSoundIDMap.remove(soundId);
        } 
	} 
	 
	public void pauseEffect(int soundId){
        Integer streamId = this.mSoundIdStreamIdMap.get(soundId);
         
        if (streamId != null && streamId.intValue() != INVALID_STREAM_ID){
        	this.mSoundPool.pause(streamId.intValue());
        	this.mPathSoundIDMap.remove(soundId);
        } 
	} 
	 
	public void resumeEffect(int soundId){
        Integer streamId = this.mSoundIdStreamIdMap.get(soundId);
         
        if (streamId != null && streamId.intValue() != INVALID_STREAM_ID){
        	this.mSoundPool.resume(streamId.intValue());
        	this.mPathSoundIDMap.remove(soundId);
        } 
	} 
	 
	public void pauseAllEffects(){ 
		// autoResume is available since level 8 
		pauseOrResumeAllEffects(true); 
	} 
	 
	public void resumeAllEffects(){ 
		// autoPause() is available since level 8 
		pauseOrResumeAllEffects(false); 
	} 
	 
	@SuppressWarnings("unchecked") 
	public void stopAllEffects(){ 
		Iterator<?> iter = this.mSoundIdStreamIdMap.entrySet().iterator();
		while (iter.hasNext()){
			Map.Entry<Integer, Integer> entry = (Map.Entry<Integer, Integer>)iter.next();
			int soundId = entry.getKey();
			this.stopEffect(soundId);
		} 
	} 
	 
	public float getEffectsVolume(){ 
		return (this.mLeftVolume + this.mRightVolume) / 2;
	} 
	 
	@SuppressWarnings("unchecked") 
	public void setEffectsVolume(float volume){
		// volume should be in [0, 1.0] 
		volume = volume / 100;
		if (volume < 0){
			volume = 0;
		}	 
		if (volume > 1){
			volume = 1;
		} 
		 
		this.mLeftVolume = this.mRightVolume = volume;
		 
		// change the volume of playing sounds 
		Iterator<?> iter = this.mSoundIdStreamIdMap.entrySet().iterator();
		while (iter.hasNext()){
			Map.Entry<Integer, Integer> entry = (Map.Entry<Integer, Integer>)iter.next();
			this.mSoundPool.setVolume(entry.getValue(), mLeftVolume, mRightVolume);
		} 
	} 
	 
	public void end(){		 
		this.mSoundPool.release();	
		this.mPathSoundIDMap.clear();	
		this.mSoundIdStreamIdMap.clear();
		 
		initData(); 
	} 
	 
	private int createSoundIdFromPath(String path){
		int soundId = INVALID_SOUND_ID;
		 
		try { 
			soundId = mSoundPool.load(sResourcePath+"/"+ path, 0);
		} catch(Exception e){
			 Log.e(TAG, "error: " + e.getMessage(), e);
		} 
		 
		return soundId;