语音提示辅助类MySoundAlertUtil

package com.jlb.scan.util;

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

import com.jlb.scan.R;

/**
 * @version V1.0
 * @Description: 语音提示的 辅助类
 */
public class MySoundAlertUtil {
    public static final int GET_ORDER_INFO_REQUEST_CODE = 0x001;
    public static final int STATUS_NORMAL = 10101; // 正常
    public static final int STATUS_PICKUP = 10102;// 取走
    public static final int STATUS_RETRIEVE = 10104;// 取回
    public static final int STATUS_WITHDRAW = 10122;// 已退
    public static final int STATUS_FLAG_WITHDRAW = 10124;// 待退
    public static final int STATUS_NOEXIT = -10999;// 不存在
    public static final int STATUS_OTHER = -10998;// 异常

    /**
     * 运单错误
     */
    public static final int ERROR_yundan_cuowu = 11;
    /**
     * 运单重复
     */
    public static final int ERROR_yundan_chongfu = 12;
    /**
     * 运单已接驳
     */
    public static final int ERROR_yundan_yijiebo = 13;
    /**
     *  接驳成功
     */
    public static final int JIEBO_SUCCESS = 14;// 异常

    public static final int ERROR_JIEBO_PICI_INFO_ERROR = 15;// 批次信息错误,请重新选择
    //运单不存在
    public static final int ERROR_WAYBILL_NOT_EXIST = 16;

    //未知
    public static final int ERROR_UNKNOW = 17;

    public static final int ERROR_NO_PHONE_NUM = 18;

    public static final int ERROR_NO_JIE_BO = 15351;

    private SoundPool mSoundPool;
    private SparseIntArray mSoundMap;
    private Context context;

    public MySoundAlertUtil(Context context) {
        super();
        this.context = context;
        initSound(context);// ;
    }

    private void initSound(Context context) {
        mSoundPool = new SoundPool(10, AudioManager.STREAM_ALARM, 5);
        mSoundMap = new SparseIntArray(20);
        mSoundMap.put(STATUS_NORMAL, mSoundPool.load(context, R.raw.normal, 1));
        mSoundMap.put(STATUS_PICKUP, mSoundPool.load(context, R.raw.finished, 1));
        mSoundMap.put(STATUS_NOEXIT, mSoundPool.load(context, R.raw.noexist, 1));
        mSoundMap.put(STATUS_OTHER, mSoundPool.load(context, R.raw.error, 1));
        mSoundMap.put(STATUS_RETRIEVE, mSoundPool.load(context, R.raw.quhui, 1));
        mSoundMap.put(STATUS_WITHDRAW, mSoundPool.load(context, R.raw.tuihui, 1));
        mSoundMap.put(STATUS_FLAG_WITHDRAW, mSoundPool.load(context, R.raw.daitui, 1));

        mSoundMap.put(ERROR_yundan_cuowu, mSoundPool.load(context, R.raw.yundan_cuowu, 1));
        mSoundMap.put(ERROR_yundan_chongfu, mSoundPool.load(context, R.raw.yundan_chongfu, 1));
        mSoundMap.put(ERROR_yundan_yijiebo, mSoundPool.load(context, R.raw.yundan_yijiebo, 1));
        mSoundMap.put(JIEBO_SUCCESS, mSoundPool.load(context, R.raw.success, 1));
        mSoundMap.put(ERROR_JIEBO_PICI_INFO_ERROR, mSoundPool.load(context, R.raw.pici_info_error, 1));
        mSoundMap.put(ERROR_WAYBILL_NOT_EXIST, mSoundPool.load(context, R.raw.waybill_not_exist, 1));
        mSoundMap.put(ERROR_NO_PHONE_NUM, mSoundPool.load(context, R.raw.no_phone_num, 1));
        mSoundMap.put(ERROR_NO_JIE_BO, mSoundPool.load(context, R.raw.no_jie_bo, 1));
    }

    public void play(int statusid) {
        if (mSoundMap.indexOfKey(statusid) != -1) {
            mSoundPool.play(mSoundMap.get(statusid), 1f, 1f, 0, 0, 1);
        }
    }

    public void release() {
        if (mSoundPool != null) {
            mSoundPool.release();
        }
    }

    /**
     * 读取文件到 sound pool
     *
     * @param resourceKey
     * @param filePath
     */
    public void loadSoundFile(int resourceKey, String filePath) {
        int id = mSoundPool.load(filePath, 1);
        mSoundMap.put(resourceKey, id);
    }

    /**
     * 从资源文件加载音频
     *
     * @param key
     * @param resouceID
     */
    public void loadFromResource(int key, int resouceID) {
        mSoundMap.put(key, mSoundPool.load(context, resouceID, 1));
    }

    public boolean exist(int soundKey) {
        return mSoundMap.indexOfKey(soundKey) > 0;
    }

    public void delete(int soundKey) {
        int soundID = mSoundMap.get(soundKey);
        if (soundID > 0) {
            mSoundPool.unload(soundID);
            mSoundMap.delete(soundKey);
        }
    }
}

 

posted @ 2016-09-19 19:21  心夢無痕  阅读(448)  评论(0)    收藏  举报