Code example for SoundPool

Methods: loadsetOnLoadCompleteListener

0
		maxVolume = (float) audioManager
				.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		volume = actualVolume / maxVolume;
 
//		loadSounds(); 
		soundPool = new SoundPool(30, AudioManager.STREAM_MUSIC, 0);
		soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
			@Override 
			public void onLoadComplete(SoundPool soundPool, int sampleId,
					int status) {
				isLoaded = true;
				// play by SoundPool 
				playSound(); 
			} 
		}); 
	} 
	public SoundPool loadSounds() {
		File tempMp3File = getMp3File(activity, word);
		 
		 
		if(tempMp3File == null){
			return null; 
		}// return 
		 
		if(soundPool !=null){	//ÒѾ­Ð½¨ÁË
			if(idFileMap.size()>=80){
				soundPool.release();
				soundPool =null;
				idFileMap.clear();
				newAsoundPool(); 
				Log.d("³¬¹ý¹æ¶¨ÒôƵÊý", "ÒѾ­ÖØн¨Á¢SoundPoll");
			} 
			Log.d("²¥·Å´ÎÊý:"+idFileMap.size(),tempMp3File.getName().substring(0, tempMp3File.getName().length()-4));
			//´Ë´¦²»Òªunload,·ñÔòÏÂÃæ²»¿ÉÖظ´Ê¹ÓÃÒÑÓÐID 
			//			soundPool.unload(id);//unload last id 
		}else{			//µÚÒ»´ÎʹÓà
			newAsoundPool(); 
 
		}//end if 
		filePath = tempMp3File.getAbsolutePath();
		id = soundPool.load(filePath,1);
		//ÏÂÃ漸ÐдúÂëÓò»ÁË£¬Ä¿µÄÊÇ£ºÖظ´Ê¹ÓÃÒÑÓеÄID(ÒѾ­½â¾ö¼ûÉÏ) 
		if(idFileMap.containsKey(filePath)){//Èç¹ûÒѾ­´æÔÚ£¬ÔòÈ¡³öID
			id = idFileMap.get(filePath);
			Log.d("²¥·ÅÒÑÓÐid = ", idFileMap.get(filePath)+"");
		}else{									 
			id = soundPool.load(filePath,1);//²»´æÔÚ£¬ÔòÉú³ÉеÄID£¬¼Óµ½MAP
			idFileMap.put(filePath, id);
			Log.d("н¨ id = "+id, filePath+"");
		} 
		return soundPool;
	}