import java.util.Enumeration; import java.util.Vector; import com.nttdocomo.io.ConnectionException; import com.nttdocomo.ui.*; import com.nttdocomo.util.Timer; import com.nttdocomo.util.TimerListener; public class MelodyPlayer extends IApplication implements ComponentListener,SoftKeyListener,TimerListener,MediaListener { private final static String[] SOUND_FILE= {"c","d","e","f","g","a","b"}; private final static String[][] SOUND_TABLE= {{"ド","ど"},{"レ","れ"},{"ミ","み"},{"ファ","ふぁ"}, {"ソ","そ"},{"ラ","ら"},{"シ","し"}}; private Panel panel; private ImageLabel title; private TextBox melody; private Button play; private VisualPresenter animation; private AudioPresenter sound; private MediaSound[] mediaSounds; private Timer timer; private String melodyString; private Vector media; // 初期化 public MelodyPlayer() { media=new Vector(); title=new ImageLabel(loadImage("/title.gif").getImage()); melody=new TextBox("",13,3,TextBox.DISPLAY_ANY); play=new Button("演奏"); animation=new VisualPresenter(); animation.setImage(loadImage("/animation.gif")); panel=new Panel(); panel.add(title); panel.add(new Label("\ue6ff楽譜",Label.LEFT)); panel.add(melody); panel.add(play); panel.add(animation); panel.setComponentListener(this); panel.setSoftLabel(Panel.SOFT_KEY_1,"終了"); panel.setSoftKeyListener(this); Display.setCurrent(panel); mediaSounds=new MediaSound[SOUND_FILE.length]; for (int i=0;i<=mediaSounds.length-1;i++) { // 実機で実行する時は .mld ファイルを使う // mediaSounds[i]=loadSound("/"+SOUND_FILE[i]+".mld"); mediaSounds[i]=loadSound("/"+SOUND_FILE[i]+".mid"); } sound=AudioPresenter.getAudioPresenter(); sound.setSound(mediaSounds[0]); sound.setMediaListener(this); } private MediaSound loadSound(String file) { MediaSound mediaSound; mediaSound=MediaManager.getSound("resource://"+file); media.addElement(mediaSound); try { mediaSound.use(); } catch (ConnectionException e) { e.printStackTrace(); } return mediaSound; } private MediaImage loadImage(String file) { MediaImage mediaImage; mediaImage=MediaManager.getImage("resource://"+file); media.addElement(mediaImage); try { mediaImage.use(); } catch (ConnectionException e) { e.printStackTrace(); } return mediaImage; } // 開始 public void start() { PhoneSystem.setAttribute( PhoneSystem.DEV_BACKLIGHT,PhoneSystem.ATTR_BACKLIGHT_ON); melodyString=""; timer=new Timer(); timer.setListener(this); timer.setRepeat(true); timer.setTime(1000); timer.start(); } // 定期処理 public void timerExpired(Timer source) { int note,skip; if (melodyString.length()==0) return; note=-1; skip=1; for (int i=0;i<=SOUND_TABLE.length-1;i++) { for (int j=0;j<=SOUND_TABLE[i].length-1;j++) { if (melodyString.startsWith(SOUND_TABLE[i][j])) { note=i; skip=SOUND_TABLE[i][j].length(); } } } melodyString=melodyString.substring(skip); sound.stop(); if (note!=-1) { sound.setSound(mediaSounds[note]); sound.play(); } } // イベント public void componentAction(Component source,int type,int param) { if (source==play && type==ComponentListener.BUTTON_PRESSED) { melodyString=melody.getText(); } } public void mediaAction(MediaPresenter source,int type,int param) { if (source==sound && type==AudioPresenter.AUDIO_PLAYING) { animation.play(); } else if (source==sound && type==AudioPresenter.AUDIO_COMPLETE) { animation.stop(); } } public void softKeyPressed(int softKey) { if (softKey==Panel.SOFT_KEY_1) { exit(); } } public void softKeyReleased(int softKey) { } // 終了 private void exit() { MediaResource resource; timer.stop(); timer.dispose(); for (Enumeration enum=media.elements();enum.hasMoreElements();) { resource=(MediaResource)(enum.nextElement()); resource.unuse(); resource.dispose(); } terminate(); } }