|
@@ -0,0 +1,134 @@
|
|
|
|
|
+package com.baoshi.piece.utils;
|
|
|
|
|
+
|
|
|
|
|
+import android.app.Activity;
|
|
|
|
|
+import android.content.Context;
|
|
|
|
|
+import android.media.AudioAttributes;
|
|
|
|
|
+import android.media.AudioManager;
|
|
|
|
|
+import android.media.SoundPool;
|
|
|
|
|
+import android.os.Build;
|
|
|
|
|
+import android.os.VibrationEffect;
|
|
|
|
|
+import android.os.Vibrator;
|
|
|
|
|
+import android.util.Log;
|
|
|
|
|
+
|
|
|
|
|
+import com.baoshi.piece.R;
|
|
|
|
|
+
|
|
|
|
|
+public class SoundManager {
|
|
|
|
|
+ private static final String TAG = "SoundManager";
|
|
|
|
|
+ private volatile static SoundManager instance;
|
|
|
|
|
+
|
|
|
|
|
+ // SoundPool 相关
|
|
|
|
|
+ private SoundPool soundPool;
|
|
|
|
|
+ private int errorSoundId;
|
|
|
|
|
+ private int scanErrorSoundId;
|
|
|
|
|
+ private int scanRepeatSoundId;
|
|
|
|
|
+ private int scanSuccessSoundId;
|
|
|
|
|
+ private int reLoginSoundId;
|
|
|
|
|
+
|
|
|
|
|
+ // 振动相关
|
|
|
|
|
+ private Vibrator vibrator;
|
|
|
|
|
+
|
|
|
|
|
+ // 单例模式
|
|
|
|
|
+ public static SoundManager getInstance() {
|
|
|
|
|
+ if (instance == null) {
|
|
|
|
|
+ synchronized (SoundManager.class) {
|
|
|
|
|
+ if (instance == null) {
|
|
|
|
|
+ instance = new SoundManager();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return instance;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 初始化方法
|
|
|
|
|
+ public void init(Context context) {
|
|
|
|
|
+ release(); // 先释放之前的资源
|
|
|
|
|
+
|
|
|
|
|
+ // 初始化SoundPool
|
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
|
+ AudioAttributes audioAttributes = new AudioAttributes.Builder()
|
|
|
|
|
+ .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
|
|
|
|
|
+ .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
|
|
|
|
+ .build();
|
|
|
|
|
+
|
|
|
|
|
+ soundPool = new SoundPool.Builder()
|
|
|
|
|
+ .setMaxStreams(4) // 同时播放的最大音效数量
|
|
|
|
|
+ .setAudioAttributes(audioAttributes)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 加载所有音效
|
|
|
|
|
+ errorSoundId = soundPool.load(context, R.raw.error, 1);
|
|
|
|
|
+ scanErrorSoundId = soundPool.load(context, R.raw.scanner_error, 1);
|
|
|
|
|
+ scanRepeatSoundId = soundPool.load(context, R.raw.scanner_repeat, 1);
|
|
|
|
|
+ scanSuccessSoundId = soundPool.load(context, R.raw.tip, 1);
|
|
|
|
|
+ reLoginSoundId = soundPool.load(context, R.raw.relogin, 1);
|
|
|
|
|
+
|
|
|
|
|
+ // 初始化振动器
|
|
|
|
|
+ vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
|
|
|
|
|
+
|
|
|
|
|
+ // 设置加载完成监听器
|
|
|
|
|
+ soundPool.setOnLoadCompleteListener((soundPool, sampleId, status) -> {
|
|
|
|
|
+ if (status == 0) {
|
|
|
|
|
+ Log.d(TAG, "音效加载完成, ID: " + sampleId);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Log.e(TAG, "音效加载失败, ID: " + sampleId);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 释放资源
|
|
|
|
|
+ public void release() {
|
|
|
|
|
+ if (soundPool != null) {
|
|
|
|
|
+ soundPool.release();
|
|
|
|
|
+ soundPool = null;
|
|
|
|
|
+ }
|
|
|
|
|
+ vibrator = null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 错误提示
|
|
|
|
|
+ public void error() {
|
|
|
|
|
+ playSound(errorSoundId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 扫描错误提示
|
|
|
|
|
+ public void scanErrorTip() {
|
|
|
|
|
+ playSound(scanErrorSoundId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 重新登录提示
|
|
|
|
|
+ public void reLoginTip() {
|
|
|
|
|
+ playSound(reLoginSoundId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 扫描重复提示(带振动)
|
|
|
|
|
+ public void scanRepeatTip() {
|
|
|
|
|
+ playSound(scanRepeatSoundId);
|
|
|
|
|
+ vibrate(1000);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 扫描成功提示
|
|
|
|
|
+ public void scanSuccessTip() {
|
|
|
|
|
+ playSound(scanSuccessSoundId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // 私有方法:播放音效
|
|
|
|
|
+ private void playSound(int soundId) {
|
|
|
|
|
+ if (soundPool != null && soundId != 0) {
|
|
|
|
|
+ soundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 私有方法:振动
|
|
|
|
|
+ private void vibrate(long milliseconds) {
|
|
|
|
|
+ if (vibrator != null && vibrator.hasVibrator()) {
|
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
+ vibrator.vibrate(VibrationEffect.createOneShot(milliseconds, VibrationEffect.DEFAULT_AMPLITUDE));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ vibrator.vibrate(milliseconds);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|