MediaPlayerが使えるのだけど、曲と曲のつなぎにフェードインフェードアウトとか、すでにその曲を再生中の時には再生しないなどの管理を毎回やるのは面倒なので、やっぱりマネージャーっぽいものを作成
- package dividebyzero.net;
- import android.media.MediaPlayer;
- public class BgmManager {
- static BgmManager instance = new BgmManager();
- private MediaPlayer player;
- private int currentPlayBgm;
- private int nextPlayBgm;
- private int vol, targetVol;
- enum PLAY_STAT{
- STOP,FADEIN,PLAYING,FADEOUT;
- }
- private PLAY_STAT stat;
- private BgmManager() {
- stat = PLAY_STAT.STOP;
- currentPlayBgm = 0;
- nextPlayBgm = 0;
- vol = 100;
- targetVol = 100;
- }
- public static BgmManager get() {
- return instance;
- }
- public static void release() {
- instance.stop();
- instance = null;
- }
- public void play(int id, boolean isFade) {
- if (id == currentPlayBgm) { // 現在再生しているので何もしない
- return;
- } else {
- nextPlayBgm = id;
- stat = PLAY_STAT.FADEOUT;
- }
- }
- public void update() {
- switch (stat) {
- case STOP:
- stop();
- if(nextPlayBgm != 0){
- currentPlayBgm = nextPlayBgm;
- nextPlayBgm = 0;
- stat = PLAY_STAT.FADEIN;
- }
- break;
- case FADEIN:
- if (player == null) {
- player = MediaPlayer.create(GLView.get().getContext(), currentPlayBgm);
- player.setLooping(true);
- player.start();
- vol = 0;
- player.setVolume(vol/100.0f, vol/100.0f);
- }
- vol += 2;
- if (targetVol < vol) {
- vol = targetVol;
- stat = PLAY_STAT.PLAYING;
- }
- player.setVolume(vol/100.0f, vol/100.0f);
- break;
- case PLAYING:
- break;
- case FADEOUT:
- if (player != null) {
- vol -= 2;
- if (vol < 0) {
- vol = 0;
- stat = PLAY_STAT.STOP;
- }
- player.setVolume(vol/100.0f, vol/100.0f);
- } else {
- stat = PLAY_STAT.STOP;
- }
- break;
- }
- }
- public void stop() { // 現在の曲を止める
- if (player != null) {
- player.stop();
- player.release();
- player = null;
- }
- }
- public void setVolume(int vol) {
- this.vol = vol;
- }
- public int getVolume() {
- return vol;
- }
- }
前の効果音の時もそうだったんだけど、リソースのデータを読みたい時って、Contextが必要になってくるので、それをいかにしてマネージャークラスに渡すかってのが悩みどころ。
僕はもうOpenGLが使えるGLViewクラスをシングルトンぽくしているので、そっからgetContext()なんだけどーー。 普通はどうやるんですかね。
0 件のコメント:
コメントを投稿