-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySound.java
47 lines (39 loc) · 1.58 KB
/
MySound.java
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
package com.example.highandlow;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
public class MySound {
private SoundPool soundPool; // 効果音を鳴らす本体
private int[] mp3s;
///////////////////////////////////////////////////
public void onCreate(Context context,int[] mp3s){
// ② 初期化(電源を入れる・コピペOK)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
} else {
AudioAttributes attr = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
soundPool = new SoundPool.Builder()
.setAudioAttributes(attr)
.setMaxStreams(5)
.build();
}
// ③ 読込処理(CDを入れる)
this.mp3s = new int[mp3s.length];
for (int i =0; i<mp3s.length; i++){
this.mp3s[i] = this.soundPool.load(context ,mp3s[i] ,1);
}
}
// ④ 再生処理(再生ボタン) //////////////////////////
public void onPlay(int number){
this.soundPool.play(this.mp3s[number], 1f,1f, 0,0, 1f);
}
// ⑤ 終了処理(電源のOFF) //////////////////////////
public void onDestroy(){
this.soundPool.release();
}
}