package com.naz.sdkdemo.utils;

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

import com.naz.sdkdemo.R;
import com.naz.sdkdemo.weight.XToast;

import java.util.HashMap;

/**
 * Description:
 * Author: yzxzm
 * Date:2021/5/12
 */
public class ScanUtils {

    private static float audioMaxVolumn,audioCurrentVolumn;
    private static float volumnRatio;
    private static AudioManager am;
    private static SoundPool soundPool;
    private  static  HashMap<Integer, Integer> soundMap = new HashMap<Integer, Integer>();
    public static void initSound(Context context){
        soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 5);
        soundMap.put(1, soundPool.load(context, R.raw.barcodebeep, 1));
        soundMap.put(2, soundPool.load(context, R.raw.serror, 1));
        am = (AudioManager) context.getSystemService(context.AUDIO_SERVICE);// 实例化AudioManager对象
        audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC); // 返回当前AudioManager对象的最大音量值
        audioCurrentVolumn = am.getStreamVolume(AudioManager.STREAM_MUSIC);// 返回当前AudioManager对象的音量值
    }

    /**
     * 播放提示音
     *
     * @param id 成功1，失败2
     */
    public static void playSound(int id) {
        volumnRatio = audioCurrentVolumn / audioMaxVolumn;
        try {
            soundPool.play(soundMap.get(id), volumnRatio, // 左声道音量
                    volumnRatio, // 右声道音量
                    1, // 优先级，0为最低
                    0, // 循环次数，0无不循环，-1无永远循环
                    1 // 回放速度 ，该值在0.5-2.0之间，1为正常速度      
            );
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

}
