package com.mq.mylibrary.rfid.io.util;


import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;

import com.mq.mylibrary.R;

import java.util.HashMap;


public class PlaySound {
		
	public static void playMedia(Context context){
//			System.out.println("media player");
			MediaPlayer player = MediaPlayer.create(context, R.raw.msg);
			if(player.isPlaying()){
//				player.release();
//				player.reset();
//				player = MediaPlayer.create(context, R.raw.msg);
				return;
			}
			try {
//				player.prepare();
				player.start();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				player.release();
			} 		
		}

		// 播放声音
		private static SoundPool soundPool;// 声音池
		private static HashMap<Integer, Integer> soundPoolMap; // 声音池中声音ID与自定义声音ID的Map

		public static void initSound(Context context) {
			// 声音池
			soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
			soundPoolMap = new HashMap<Integer, Integer>();
			// 吃东西音乐
			soundPoolMap.put(6, soundPool.load(context, R.raw.msg, 1)); // 消息
		}
		
		public static void playSound(Context context, int sound, int loop) {
			AudioManager mgr = (AudioManager) context 
					.getSystemService(Context.AUDIO_SERVICE);
			float streamVolumeCurrent = mgr
					.getStreamVolume(AudioManager.STREAM_MUSIC);
			float streamVolumeMax = mgr
					.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
			float volume = streamVolumeCurrent / streamVolumeMax;
			soundPool.play(soundPoolMap.get(sound), volume, volume, 1, loop, 1f);
		}		
		

}
