Code example for SoundPool

Methods: loadplay

0
		if(!sPath2SoundIdMap.containsKey(path)) {
			AssetFileDescriptor afd = null;
			int soundId = -1;
			try { 
				if(isFile)
					soundId = sPool.load(path, 0);
				else { 
					Context context = Director.getInstance().getContext();
					AssetManager am = context.getAssets();
					afd = am.openFd(path);
					soundId = sPool.load(afd, 0);
				} 
				if(soundId != -1) {
					sPath2SoundIdMap.put(path, soundId);
				} 
			} catch (IOException e) {
			} finally { 
				try { 
					if(afd != null)
						afd.close();
				} catch (IOException e) {
				} 
			} 
		} 
	} 
	 
	synchronized static void mpPlayEffect(int resId) {
		if(sPool == null || sResId2SoundIdMap == null)
			return; 
		 
		mpPreloadEffect(resId);
		Integer soundId = sResId2SoundIdMap.get(resId);
		if(soundId != null) {
			int streamId = sPool.play(soundId, sMute ? 0 : sEffectVolume, sMute ? 0 : sEffectVolume, 0, 0, 1);
			if(streamId == 0) {
				Log.w("libwiengine", "Failed to play sound resId: " + resId);
				return; 
			} 
			 
			List<Integer> streamIds = sSoundId2StreamIdMap.get(soundId);
			if(streamIds == null) {
				streamIds = new ArrayList<Integer>();
				sSoundId2StreamIdMap.put(soundId, streamIds);
			} 
			streamIds.add(streamId);
		} 
	} 
	 
	synchronized static void mpPlayEffect(String path, boolean isFile) {
		if(sPool == null || sPath2SoundIdMap == null)
			return; 
		 
		mpPreloadEffect(path, isFile);
		Integer soundId = sPath2SoundIdMap.get(path);
		if(soundId != null) {
			int streamId = sPool.play(soundId, sMute ? 0 : sEffectVolume, sMute ? 0 : sEffectVolume, 0, 0, 1);
			if(streamId == 0) {
				Log.w("libwiengine", "Failed to play sound path: " + path);
				return; 
			}