package com.ifavine.pay.utils;

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

import com.ifavine.pay.R;

import java.util.HashMap;

/**
 * Created by Administrator on 2018/12/20.
 */

public class SoundPlayUtils {
    // SoundPool对象
    public static SoundPool mSoundPlayer = new SoundPool(20,
            AudioManager.STREAM_MUSIC, 5);
    public static SoundPlayUtils soundPlayUtils;
    // 上下文
    static Context mContext;
    private static HashMap<Integer, Integer> data = new HashMap<Integer, Integer>();

    /**
     * 初始化
     * 初始化音乐池  播发喇叭的   BaseApplication初始化 方法里面用
     *
     * @param context
     */
    public static SoundPlayUtils init(Context context) {
        if (soundPlayUtils == null) {
            soundPlayUtils = new SoundPlayUtils();
        }
        // 初始化声音
        mContext = context;
        /**
         * 参数1:加载音乐流第多少个 (只用了俩个音乐) 2:设置音乐的质量 音乐流 3:资源的质量  0
         */
        data.put(200, mSoundPlayer.load(mContext, R.raw.success, 1));
        data.put(1, mSoundPlayer.load(mContext, R.raw.fail, 1));
        data.put(500, mSoundPlayer.load(mContext, R.raw.s500, 1));
        data.put(501, mSoundPlayer.load(mContext, R.raw.s501, 1));
        data.put(502, mSoundPlayer.load(mContext, R.raw.s502, 1));
        data.put(503, mSoundPlayer.load(mContext, R.raw.s503, 1));
        data.put(504, mSoundPlayer.load(mContext, R.raw.s504, 1));
        data.put(505, mSoundPlayer.load(mContext, R.raw.s505, 1));
        data.put(506, mSoundPlayer.load(mContext, R.raw.s506, 1));
        data.put(507, mSoundPlayer.load(mContext, R.raw.s507, 1));
        data.put(508, mSoundPlayer.load(mContext, R.raw.s508, 1));
        data.put(509, mSoundPlayer.load(mContext, R.raw.s509, 1));
        data.put(510, mSoundPlayer.load(mContext, R.raw.s510, 1));
        data.put(511, mSoundPlayer.load(mContext, R.raw.s511, 1));
        data.put(512, mSoundPlayer.load(mContext, R.raw.s512, 1));
        data.put(513, mSoundPlayer.load(mContext, R.raw.s513, 1));
        data.put(514, mSoundPlayer.load(mContext, R.raw.s514, 1));
        data.put(515, mSoundPlayer.load(mContext, R.raw.s515, 1));
        return soundPlayUtils;
    }

    /**
     * 播放声音
     *
     * @param soundID 设置声音
     */
    public static void play(int soundID) {
        if (data.containsKey(soundID)) {
            mSoundPlayer.play(data.get(soundID), 1, 1, 0, 0, 1);
        } else {
            mSoundPlayer.play(data.get(1), 1, 1, 0, 0, 1);
        }
    }

    public static void release() {
        mSoundPlayer.release();
    }
}

