/* iアプリ, MIDP でのサウンド Time-stamp: "2004/11/10 Wed 22:43 hig" Saburo Higuchi 2003 http://www.math.ryukoku.ac.jp/~hig/ プログラム解説等 http://sparrow.math.ryukoku.ac.jp/~hig/course/juniors_2003/08/ */ 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 { IApplication parent; String message="012を押してね"; final int nfile=3; String [] filename; int initial=1; MediaSound [] ms; AudioPresenter ap; boolean onEmulator=true; SoundCanvas(IApplication parent){ this.parent=parent; ap=AudioPresenter.getAudioPresenter(); ms = new MediaSound[nfile]; // サウンドファイルの読み込み filename = new String [nfile]; if ( onEmulator ){ // DoJa 3.5(FOMA 900i)以降ではここをtrueにしてよい. filename[0]="nozomi.mid"; filename[1]="hikari.mid"; filename[2]="hikaritotyu.mid"; } else { 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){ } // 演奏開始 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(); message = "1キーが押された"; } else if ( type==Display.KEY_RELEASED_EVENT && param==Display.KEY_2 ){ ap.stop(); ap.setSound(ms[1]); ap.play(); message = "2キーが押された"; } else if ( type==Display.KEY_PRESSED_EVENT && param==Display.KEY_3 ){ ap.stop(); ap.setSound(ms[2]); ap.play(); message = "3キーが押された"; } repaint(); // paint メソッドを呼ぶ. } } /* Local Variables: mode: java End: */