1
wxr
2023-04-23 2cd55265ccff3b0a267d7953b2dd9e5dca437aa6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* 
 * @ProjectName iVMS-5060_V3.0
 * @Copyright null
 * 
 * @FileName AudioPlayUtil.java
 * @Description 这里对文件进行描述
 * 
 * @author mlianghua
 * @data Jun 28, 2012
 * 
 * @note 这里写本文件的详细功能描述和注释
 * @note 历史记录
 * 
 * @warning 这里写本文件的相关警告
 */
package com.videogo.ui.util;
 
import android.app.Application;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
 
import java.util.HashMap;
 
import ezviz.ezopensdk.R;
 
 
public class AudioPlayUtil {
 
    private SoundPool mSoundPool = null;
 
    public static int CAPTURE_SOUND = 1;
 
    public static int RECORD_SOUND = 2;
 
    private boolean mRingerMode = true;
 
    private int mStreamID = 0;
 
    private Context mContext = null;
 
    private HashMap<Integer, Integer> mSoundMap = null;
 
    private static AudioPlayUtil mAudioPlayUtil = null;
 
    private AudioPlayUtil(Application application) {
        mContext = application.getApplicationContext();
        mSoundMap = new HashMap<Integer, Integer>();
 
        mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 100);
        mSoundMap.put(CAPTURE_SOUND, mSoundPool.load(mContext, R.raw.paizhao, 0));
        mSoundMap.put(RECORD_SOUND, mSoundPool.load(mContext, R.raw.record, 0));
    };
 
    public static AudioPlayUtil getInstance(Application application) {
        if (mAudioPlayUtil == null) {
            mAudioPlayUtil = new AudioPlayUtil(application);
        }
 
        return mAudioPlayUtil;
    }
 
    public void playAudioFile(int soundId) {
        stopAudioPlay();
        getAlarmParams();
        if (mRingerMode) {
            mStreamID = mSoundPool.play(mSoundMap.get(soundId), 1, 1, 0, 0, 1);
            if (mStreamID == 0 && (soundId == 3 || soundId == 4)) {
                mStreamID = mSoundPool.play(mSoundMap.get(soundId + 2), 1, 1, 0, 0, 1);
            }
        }
    }
 
    public void stopAudioPlay() { 
        if (mStreamID != 0) {
            mSoundPool.stop(mStreamID);
        }
    }
 
    private void getAlarmParams() {
        // AudioManager provides access to volume and ringer mode control.
        AudioManager volMgr = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        int ringerMode = volMgr.getRingerMode();
        if (ringerMode == AudioManager.RINGER_MODE_SILENT || ringerMode == AudioManager.RINGER_MODE_VIBRATE) {
            mRingerMode = false;
        } else if (ringerMode == AudioManager.RINGER_MODE_NORMAL) {
            mRingerMode = true;
        }
    }
}