01/08/2012
SoundPool là 1 công cụ trên Android giúp các bạn có thể mở nhiều file Audio vào cùng 1 thời điểm. Trong phần demo này mình sẽ tạo nên 1 ứng dụng chạy đồng thời 4 file MP3 trên Virtual Device . Các bước để tạo ứng dụng như sau:
1/ Tạo Project :
Project name: SoundPoolDemo
Build Target: Android 2.3.3
Application name: SoundPoolDemo
Package name: com.dac.soundpool
Create Activity: MainActivity
2/ Trong file main.xml các bạn thiết kế giao diện đơn giản như sau:
PHP Code
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
>
<ToggleButton android:id="@+id/button"
android:textOn="Pause" android:textOff="Resume"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:onClick="doClick" android:checked="true" />
</LinearLayout>
3/ Các bạn tạo thêm 1 folder raw trong res/ và add vào đó 4 file nhạc MP3. Mình đặt tên lần lượt là : chimp.mp3, crickets.mp3, rooster.mp3, music_flie.mp3
4/ Trong file MainActivity.java các bạn code xử lý như sau:
PHP Code:
package com.dac.soundpool
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ToggleButton;
public class MainActivity extends Activity implements SoundPool.OnLoadCompleteListener {
private static final int SRC_QUALITY = 0;
private static final int PRIORITY = 1;
private SoundPool soundPool = null;
private AudioManager aMgr;
private int sid_background;
private int sid_roar;
private int sid_bark;
private int sid_chimp;
private int sid_rooster;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onResume() {
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, SRC_QUALITY);
soundPool.setOnLoadCompleteListener(this);
aMgr = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
sid_background = soundPool.load(this, R.raw.crickets, PRIORITY);
sid_chimp = soundPool.load(this, R.raw.chimp, PRIORITY);
sid_rooster = soundPool.load(this, R.raw.rooster, PRIORITY);
sid_roar = soundPool.load(this, R.raw.music_flie, PRIORITY);
try {
AssetFileDescriptor afd = this.getAssets().openFd("dogbark.mp3");
sid_bark = soundPool.load(afd.getFileDescriptor(), 0, afd.getLength(), PRIORITY);
afd.close();
} catch (IOException e) {
e.printStackTrace();
}
super.onResume();
}
public void doClick(View view) {
switch(view.getId()) {
case R.id.button:
if(((ToggleButton)view).isChecked()) {
soundPool.autoResume();
}
else {
soundPool.autoPause();
}
break;
}
}
@Override
protected void onPause() {
soundPool.release();
soundPool = null;
super.onPause();
}
@Override
public void onLoadComplete(SoundPool sPool, int sid, int status) {
Log.v("soundPool", "sid " + sid + " loaded with status " + status);
final float currentVolume = ((float)aMgr.getStreamVolume(AudioManager.STREAM_MUSIC)) /
((float)aMgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
if(status != 0)
return;
if(sid == sid_background) {
if(sPool.play(sid, currentVolume, currentVolume,
PRIORITY, -1, 1.0f) == 0)
Log.v("soundPool", "Failed to start sound");
} else if(sid == sid_chimp) {
queueSound(sid, 5000, currentVolume);
} else if(sid == sid_rooster) {
queueSound(sid, 6000, currentVolume);
} else if(sid == sid_roar) {
queueSound(sid, 12000, currentVolume);
} else if(sid == sid_bark) {
queueSound(sid, 7000, currentVolume);
}
}
private void queueSound(final int sid, final long delay, final float volume) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(soundPool == null) return;
if(soundPool.play(sid, volume, volume,
PRIORITY, 0, 1.0f) == 0)
Log.v("soundPool", "Failed to start sound (" + sid + ")");
queueSound(sid, delay, volume);
}}, delay);
}
}
|

