ADD file via upload

master
p10297854 4 years ago
parent 54b2db37c5
commit 15abe783ea

@ -0,0 +1,57 @@
package com.example.musicwork.chuangguan;
import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;
import com.example.musicwork.R;
public class PianoMusic {
//由于资源包含了钢琴的全部音效,特此备注:资源里与琴键的对应为:
//c2 - C; c3 - c; c4 - c1(中央C); c5 - c2
int Music[] = {
R.raw.c3, R.raw.d3, R.raw.e3, R.raw.f3, R.raw.g3, R.raw.a3, R.raw.b3,
R.raw.c4, R.raw.d4, R.raw.e4, R.raw.f4, R.raw.g4, R.raw.a4, R.raw.b4,
R.raw.c5, R.raw.d5, R.raw.e5, R.raw.f5, R.raw.g5, R.raw.a5, R.raw.b5,
};
//使用soundpool播放音效
SoundPool soundPool;
HashMap<Integer, Integer> soundPoolMap;
//加载ID
public PianoMusic(Context context) {
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new HashMap<Integer, Integer>();
//加载音效ID到SoundPoolMap
for (int i = 0; i < Music.length; i++) {
soundPoolMap.put(i, soundPool.load(context, Music[i], 1));
}
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
Log.e("map", "音频池资源id为" + sampleId + "的资源装载完成");
}
});
}
//播放
public void soundPlay(int no) {
soundPool.play(soundPoolMap.get(no), 1,1, 1, 0, 1.0f);
}
//结束
public int soundOver() {
return soundPool.play(soundPoolMap.get(1), 100, 100, 1, 0, 1.0f);
}
@Override
protected void finalize() throws Throwable {
soundPool.release();
super.finalize();
}
}
Loading…
Cancel
Save