Code example for SoundPool

Methods: loadsetVolumeunload

0
				player = new MediaPlayer();
				player.setDataSource(fileName);
				player.prepare();
			}else{ 
				if(soundID!=-1) unloadSound();
				soundID = pool.load(fileName, 1);
			} 
			setVolume(volume);
			bIsLoaded = true;
			this.fileName = fileName;
			this.stream = stream;
		} catch (Exception e) {
			Log.e("OF","couldn't load " + fileName,e);
		}  
	} 
	 
	void unloadSound(){ 
		if(player!=null){
			player.release();
			player = null;
		} 
 
		if(soundID!=-1){
			pool.unload(soundID);
		} 
		fileName = null;
		soundID = -1;
		streamID = -1;
		bIsLoaded = false;
		bIsPlaying = false;
	} 
	 
	void play(){ 
		if(stream){
			if(player==null) return;
			if(getIsPlaying()) setPosition(0);	 
			player.start();
		}else{ 
			if(!multiPlay){
				pool.stop(streamID);
			} 
			streamID = pool.play(soundID,leftVolume,rightVolume,1,loop?-1:0,speed);
			bIsPlaying = true;
		} 
	} 
	void stop(){ 
		if(stream){
			if(player==null) return;
			player.stop();
		}else if(streamID!=-1){
			pool.stop(streamID);
			bIsPlaying = false;
		} 
	} 
	 
	void setVolume(float vol){
		volume = vol;
		// calculates left/right volumes from pan-value (constant panning law)  
		// see: Curtis Roads: Computer Music Tutorial p 460 
		// thanks to jasch 
		float angle = pan * 0.7853981633974483f; // in radians from -45. to +45.
		float cosAngle = FloatMath.cos(angle);
		float sinAngle = FloatMath.sin(angle);
		leftVolume  = (float)((cosAngle - sinAngle) * 0.7071067811865475) * vol; // multiplied by sqrt(2)/2
		rightVolume = (float)((cosAngle + sinAngle) * 0.7071067811865475) * vol; // multiplied by sqrt(2)/2
		if(stream){
			if(player!=null) player.setVolume(leftVolume, rightVolume);
		}else if(streamID!=-1){
			pool.setVolume(streamID, leftVolume, rightVolume);
		} 
	} 
	 
	float getVolume(){ 
		return volume;