2010年11月11日木曜日

androidでBGM再生

効果音はできたので、今度はBGM
MediaPlayerが使えるのだけど、曲と曲のつなぎにフェードインフェードアウトとか、すでにその曲を再生中の時には再生しないなどの管理を毎回やるのは面倒なので、やっぱりマネージャーっぽいものを作成


  1. package dividebyzero.net;  
  2.   
  3. import android.media.MediaPlayer;  
  4.   
  5. public class BgmManager {  
  6.     static BgmManager instance = new BgmManager();  
  7.     private MediaPlayer player;  
  8.     private int currentPlayBgm;  
  9.     private int nextPlayBgm;  
  10.     private int vol, targetVol;  
  11.     enum PLAY_STAT{  
  12.         STOP,FADEIN,PLAYING,FADEOUT;  
  13.     }  
  14.     private PLAY_STAT stat;  
  15.   
  16.     private BgmManager() {  
  17.         stat = PLAY_STAT.STOP;  
  18.         currentPlayBgm = 0;  
  19.         nextPlayBgm = 0;  
  20.         vol = 100;  
  21.         targetVol = 100;  
  22.     }  
  23.   
  24.     public static BgmManager get() {  
  25.         return instance;  
  26.     }  
  27.   
  28.     public static void release() {  
  29.         instance.stop();  
  30.         instance = null;  
  31.     }  
  32.   
  33.     public void play(int id, boolean isFade) {  
  34.         if (id == currentPlayBgm) { // 現在再生しているので何もしない  
  35.             return;  
  36.         } else {  
  37.             nextPlayBgm = id;  
  38.             stat = PLAY_STAT.FADEOUT;  
  39.         }  
  40.     }  
  41.   
  42.     public void update() {  
  43.         switch (stat) {  
  44.             case STOP:  
  45.                 stop();   
  46.                 if(nextPlayBgm != 0){  
  47.                     currentPlayBgm = nextPlayBgm;  
  48.                     nextPlayBgm = 0;  
  49.                     stat = PLAY_STAT.FADEIN;   
  50.                 }  
  51.                 break;  
  52.             case FADEIN:  
  53.                 if (player == null) {  
  54.                     player = MediaPlayer.create(GLView.get().getContext(), currentPlayBgm);  
  55.                     player.setLooping(true);  
  56.                     player.start();  
  57.                     vol = 0;  
  58.                     player.setVolume(vol/100.0f, vol/100.0f);  
  59.                 }  
  60.                 vol += 2;  
  61.                 if (targetVol < vol) {  
  62.                     vol = targetVol;  
  63.                     stat = PLAY_STAT.PLAYING;  
  64.                 }  
  65.                 player.setVolume(vol/100.0f, vol/100.0f);  
  66.                 break;  
  67.             case PLAYING:  
  68.                 break;  
  69.             case FADEOUT:  
  70.                 if (player != null) {  
  71.                     vol -= 2;  
  72.                     if (vol < 0) {  
  73.                         vol = 0;  
  74.                         stat = PLAY_STAT.STOP;  
  75.                     }  
  76.                     player.setVolume(vol/100.0f, vol/100.0f);  
  77.                 } else {  
  78.                     stat = PLAY_STAT.STOP;  
  79.                 }  
  80.                 break;  
  81.         }  
  82.     }  
  83.   
  84.     public void stop() { // 現在の曲を止める  
  85.         if (player != null) {  
  86.             player.stop();  
  87.             player.release();  
  88.             player = null;  
  89.         }  
  90.     }  
  91.   
  92.     public void setVolume(int vol) {  
  93.         this.vol = vol;  
  94.     }  
  95.   
  96.     public int getVolume() {  
  97.         return vol;  
  98.     }  
  99. }  

前の効果音の時もそうだったんだけど、リソースのデータを読みたい時って、Contextが必要になってくるので、それをいかにしてマネージャークラスに渡すかってのが悩みどころ。

僕はもうOpenGLが使えるGLViewクラスをシングルトンぽくしているので、そっからgetContext()なんだけどーー。 普通はどうやるんですかね。

0 件のコメント:

コメントを投稿