研究テーマ->音楽ソフトの開発->携帯用アプリ開発->DoCoMo iアプリ->出目に従って演奏を行わせる  
  携帯電話用アプリケーションの開発について紹介します。  
プロジェクトの準備
iαppli Development Kit for DoJa-5.0を起動します。
前の項目までに作成した「DiceMusic」を読み込みます。これに、演奏を行う機能を追加します。
ソースコードの追記
下記のようにMainCanvas.javaを書き換えます。
import com.nttdocomo.ui.*;
import java.util.Random;

class MainCanvas extends Canvas implements MediaListener
{
    int dice[] = new int[8*2];
    Random rnd = new Random();
    boolean init_flg = false;
    AudioPresenter ap;
    int play_index;
    MediaSound sound[] = new MediaSound[187];
    int step = 0;

     public MainCanvas()
    {
        setSoftLabel(Frame.SOFT_KEY_1,"サイコロ");
        setSoftLabel(Frame.SOFT_KEY_2,"終了");
        ap = AudioPresenter.getAudioPresenter();
        ap.setMediaListener(this);
        try{
            int num_sound_file = 0;
            int i;
            for(i=0;i<2;i++)
            {
                 String Part = new String();
                 Part = "";

                 if(i==0)
                 {
                      Part = "A";
                 }
                 else
                 {
                      Part = "B";
                 }

                 int j;
                 for(j=2;j<=12;j++)
                 {
                      String Dice = new String();
                      Dice = "";

                      if(j<10)
                      {
                          Dice = Dice + "0";
                      }
                      Dice = Dice + Integer.toString(j);
                      int k;
                      for(k=1;k<=9;k++)
                      {

                           String Bar = new String();
                           Bar = "";

                           if(i==1 && k==9)
                           {
                                continue;
                           }
                           Bar = Bar + Integer.toString(k);

                           sound[num_sound_file] =
                                       MediaManager.getSound("resource:///sound/"+Part+Dice+Bar+".MLD");
                           sound[num_sound_file].use();
                           num_sound_file++;


                      }
                 }

            }

        }catch(Exception s){
             Dialog dialog = new Dialog(Dialog.DIALOG_INFO, "エラー");
             dialog.setText("Soundでエラーが発生。");
             dialog.show();
             IApplication.getCurrentApp().terminate();
        }
    }

    public void paint( Graphics g )
    {
       if(init_flg == false)
       {
            init_flg = true;
            return;
       }
       
       g.clearRect(0,0,200,2*8*13+30);
      
       int i;
       for(i=0;i<8*2;i++)
       {
            g.drawString(Integer.toString(i+1)+"回目のサイコロ",0,i*13+30);
            g.drawString(Integer.toString(dice[i]),100,i*13+30);
       }
    }
    public void processEvent( int type, int param )
    {
        if( type == Display.KEY_PRESSED_EVENT )
        {
            if( param == Display.KEY_SOFT1 )
            {
                if(step==0)
                {
                    step = 1;
                    int i;
                    for(i=0;i<8*2;i++)
                    {
                         dice[i] = rnd.nextInt(11)+2;
                    }
                    setSoftLabel(Frame.SOFT_KEY_1,"再生");
                    repaint();
                }
                else if(step==1)
                {
                    step = 2;
                    play_index = 0;
                    int idx =  getSoundIndex();

                    ap.setSound(sound[idx]);
                    ap.play();
                    setSoftLabel(Frame.SOFT_KEY_1,"停止");
                 }
                 else if(step==2)
                 {
                    step = 0;
                    ap.stop();
                    setSoftLabel(Frame.SOFT_KEY_1,"サイコロ");
                 }

            }
            else if( param == Display.KEY_SOFT2 )
            {
                if(step==2)
                {
                    ap.stop();
                }
                int i;
                for(i=0;i<187;i++)
                {
                     sound[i].unuse();
                     sound[i].dispose();
                }
 
                IApplication.getCurrentApp().terminate();
            }
        }
    }

    private int getSoundIndex()
    {
       int idx = 0;
       if(play_index <16)
       {
           if(play_index ==15)
           {
                idx = 9*(dice[play_index%8]-2) + 8;
           }
           else
           {
                idx = 9*(dice[play_index%8]-2) + play_index%8;
           }
       }
       else if(play_index <24)
       {
            idx = 99 + 8*(dice[play_index-8]-2)+(play_index-16);
       }
       return idx;
    }

    public void mediaAction( MediaPresenter source, int type, int param )
    {           
        if( type == AudioPresenter.AUDIO_COMPLETE )
        {
             play_index = play_index + 1;
             if(play_index <24)
             {
                 int idx =  getSoundIndex();
                 ap.setSound(sound[idx]);
                 ap.play();
             }
             else
             {
                 step = 0;
                 ap.stop();
                 setSoftLabel(Frame.SOFT_KEY_1,"サイコロ");
             }

        }
    }
 }
[起動]ボタンをクリックして、エミュレータ画面を呼び出します。サイコロの後、再生ボタンを押すと、曲が再生されます。