a
554325746@qq.com
2019-12-25 38492bbaa63586e2f4877da0eaa01a082fd565a6
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
package com.basic.security.utils;
 
import android.media.MediaPlayer;
 
import com.basic.security.base.BaseApplication;
import com.basic.security.fragment.SettingFragment;
import com.basic.security.fragment.helper.HomePlayAlarmSound;
 
public class AudioPlayer {
 
 
    public static MediaPlayer play(MediaPlayer mp, String alarmAudio, boolean looping) {
        try {
            if (mp == null) {
                mp = MediaPlayer.create(BaseApplication.getApplication().activity, SettingFragment.getAudioRawIndex(alarmAudio));
                mp.setLooping(looping);
                mp.start();
            } else {
                if (!mp.isPlaying() || !looping || HomePlayAlarmSound.shouldRestartSound) {
                    if (HomePlayAlarmSound.shouldRestartSound) {
                        HomePlayAlarmSound.shouldRestartSound = false;
                    }
                    mp.stop();
                    mp.release();
                    mp = MediaPlayer.create(BaseApplication.getApplication().activity, SettingFragment.getAudioRawIndex(alarmAudio));
                    mp.setLooping(looping);
                    mp.start();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return mp;
    }
 
    public static void stopInNewThread(final MediaPlayer mp) {
        new Thread() {
            @Override
            public void run() {
                AudioPlayer.stop(mp);
            }
        }.start();
    }
 
    public static void stop(MediaPlayer mp) {
        try {
            if (mp != null && mp.isPlaying()) {
                mp.stop();
                mp.release();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
}