/* iアプリ, MIDP でのサウンド Time-stamp: "2006/01/25 Wed 20:04 hig" Saburo Higuchi 2003-2005 http://www.math.ryukoku.ac.jp/~hig/ プログラム解説等 http://www.a.math.ryukoku.ac.jp/~hig/course/juniors_2005/07/ */ import com.nttdocomo.ui.*; /** IApplication または MIDlet を継承するクラス */ public class SoundSample extends IApplication { /** 起動するときに呼ばれる メソッド. 必須. */ public void start(){ SoundCanvas cc=new SoundCanvas(this); Display.setCurrent(cc); } } class SoundCanvas extends Canvas { SoundSample parent; String message="123を押してね"; final int nfile=3; String [] filename; int initial=1; int melody; MediaSound [] ms; AudioPresenter ap; boolean onEmulator=true; SoundCanvas(SoundSample parent){ this.parent=parent; ap=AudioPresenter.getAudioPresenter(); ms = new MediaSound[nfile]; // サウンドファイルの読み込み filename = new String [nfile]; if ( onEmulator ){ // DoJa 3.5(FOMA 900i)以降ではここをtrueにしてよい. // MIDI ファイル filename[0]="nozomi.mid"; filename[1]="hikari.mid"; filename[2]="hikaritotyu.mid"; } else { // iメロディ filename[0]="nozomi.mld"; filename[1]="hikari.mld"; filename[2]="hikaritotyu.mld"; } for(int i=0; i< nfile ; i++){ ms[i]=MediaManager.getSound("resource:///" + filename[i] ); } try{ for(int i=0; i< nfile ; i++){ ms[i].use(); } } catch (Exception e){ // e.printStackTrace(); } // 演奏開始 if( initial >=0 && initial < nfile ){ melody=initial; ap.setSound(ms[initial]); ap.play(); } } public void paint(Graphics g){ g.setColor(Graphics.getColorOfName(Graphics.WHITE)); g.fillRect(0,0,getWidth(),getHeight()); // 画面を消す g.setColor(Graphics.getColorOfName(Graphics.BLACK)); g.drawString(message,0,getHeight() +0); } public void processEvent(int type, int param){ if( type==Display.KEY_PRESSED_EVENT && param==Display.KEY_1 ){ ap.stop(); ap.setSound(ms[0]); ap.play(); melody=0; message = "1キーが押された"; } else if ( type==Display.KEY_PRESSED_EVENT && param==Display.KEY_2 ){ ap.stop(); ap.setSound(ms[1]); ap.play(); melody=1; message = "2キーが押された"; } else if ( type==Display.KEY_PRESSED_EVENT && param==Display.KEY_3 ){ ap.stop(); ap.setSound(ms[2]); ap.play(); melody=2; message = "3キーが押された"; } repaint(); // paint メソッドを呼ぶ. } } /* Local Variables: mode: java End: */