Android SoundPool封装

SoundPool简介

SoundPool常用来同时播放多个短暂的音频

封装

这里封装一个简单的SoundPlayer,模拟管理播放王者荣耀里的单杀、双杀、和三杀的音频,支持播放、循环播放、暂停、继续播放等功能

所需的三个音频文件sound_single_kill、sound_double_kill、sound_triple_kill放置在/res/raw文件夹下

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
91
92
93
94
95
96
97
98
99
public class SoundPlayer {

private Context context;
private SoundPool soundPool;
// 能同时播放的最大声音数
private static final int MAX_SREAMS = 5;
// 单杀的声音
public static int SOUND_SINGLE_KILL;
// 双杀的声音
public static int SOUND_DOUBLE_KILL;
// 三杀的声音
public static int SOUND_TRIPLE_KILL;

public SoundPlayer(Context context) {
this(context, null);
}

public SoundPlayer(Context context, SoundPool.OnLoadCompleteListener onLoadCompleteListener) {
this.context = context;
soundPool = new SoundPool(MAX_SREAMS, AudioManager.STREAM_MUSIC,0);
SOUND_SINGLE_KILL = soundPool.load(context, R.raw.sound_single_kill, 1);
SOUND_DOUBLE_KILL = soundPool.load(context, R.raw.sound_double_kill, 1);
SOUND_TRIPLE_KILL = soundPool.load(context, R.raw.sound_triple_kill, 1);
soundPool.setOnLoadCompleteListener(onLoadCompleteListener);
}

// resId为放在raw文件夹下的音频文件
public void load(int resId) {
soundPool.load(context, resId, 1);
}

// 参数为SoundPool.load()方法返回的soundID
public void unload(int soundID) {
soundPool.unload(soundID);
}

// 播放单杀的声音
public void playSingleKill() {
play(SOUND_SINGLE_KILL);
}

// 播放双杀的声音
public void playDoubleKill() {
play(SOUND_DOUBLE_KILL);
}

// 播放三杀的声音
public void playTripleKill() {
play(SOUND_TRIPLE_KILL);
}

// 播放,soundID参数为SoundPool.load()方法返回的值
public int play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) {
return soundPool.play(soundID, leftVolume, rightVolume, priority, loop, rate);
}

// 默认参数的播放
public int play(int soundID) {
return play(soundID, 1, 1, 0, 0, 1);
}

// 循环播放
public int loopPlay(int soundID) {
return play(soundID, 1, 1, 0, -1, 1);
}

// 停止播放
public void stop(int streamId) {
soundPool.stop(streamId);
}

// 暂停播放
public void pausea(int streamId) {
soundPool.pause(streamId);
}

// 继续播放
public void resume(int streamId) {
soundPool.resume(streamId);
}

// 暂停所有播放
public void pauseAll() {
soundPool.autoPause();
}

// 继续所有播放
public void resumeAll() {
soundPool.autoResume();
}

// 释放
public void release() {
if (soundPool != null) {
soundPool.release();
soundPool = null;
}
}
}

Click and drag to move

使用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public void test(Context context) {
// 实例化SoundPlayer
SoundPlayer player = new SoundPlayer(context);
// 播放单杀音频
player.playSingleKill();
// 暂停播放单杀音频
player.pausea(SoundPlayer.SOUND_SINGLE_KILL);
// 继续播放单杀音频
player.resume(SoundPlayer.SOUND_SINGLE_KILL);
// 停止播放单杀音频
player.stop(SoundPlayer.SOUND_SINGLE_KILL);
// 播放双杀音频
player.playDoubleKill();
// 播放三杀音频
player.playTripleKill();
// 暂停播放所有音频
player.pauseAll();
// 继续播放所有音频
player.resumeAll();
// 释放
player.release();
}

Click and drag to move