You are on page 1of 11

THC HNH CHUYN 1

LI M U
Cng ngh thng tin ngy cng i su vo cuc sng con ngi , c bit l s pht trin ca cc thit b cm tay nh gn, di ng nhng c rt nhiu ng dng rt a dng v thit thc cho con ngi nh lin lc, truy cp Internet, nghe nhc, chp nh chi game in thoi di ng l mt trong nhng thit b nh vy. Do nhu cu s dng ngy cng tng nn nhng ng dng i km vi n cng khng ngng pht trin. T ngnh lp trnh cho mobile ra i v ngy cng pht trin mnh m. L sinh vin khoa CNTT chng em may mn c sm lm quen v tip cn vi lnh vc lp trnh cho mobile qua rt nhiu cc mn hc th v bo gm c l thuyt v thc hnh m m u l mn Chuyn 1. p ng yu cu ca mn hc cng nh bc u i vo tm hiu v lp trnh Mobile v J2ME tha mn nim am m khm ph, chng em xin c nghin cu v tp xy dng mt ng dng nh cho in thoi di ng qua ti : Xy dng ng dng chi nhc n gin cho in thoi di ng. Chng em chn thnh cm n thy gio Trnh Cng Duy tn tm ging dy v hng dn chng em thc hin ti ny.

Nhm 7B_Lp 09T4

Trang 1

THC HNH CHUYN 1

I. Khi qut v J2ME


1. Lch s
J2ME c pht trin t kin trc Java Card, Embeded Java v Personal Java ca phin bn Java 1.1. n s ra i ca Java 2 th Sun quyt nh thay th Personal Java v c gi vi tn mi l Java 2 Micro Edition, hay vit tt l J2ME. ng vi tn gi, J2ME l nn tng cho cc thit b c tnh cht nh gn.

2. L do chn J2ME
Java ban u c thit k dnh cho cc my vi ti nguyn b nh hn ch. Th trng ca J2ME c m rng ra cho nhiu chng loi thit b nh: Cc loi th c nhn nh Java Card
My in thoi di ng My PDA (Personal Digital Assistant - thit b tr gip c nhn) Cc hp iu khin dnh cho tivi, thit b gii tr gia dng

3. Kin trc ca J2ME (1)


Cc thnh phn trong nn tng J2ME

nh ngha v Configuration (Cu hnh): l c t nh ngha mt mi trng phn mm cho mt dng cc thit b c phn loi bi tp hp cc c tnh, v d nh: Kiu v s lng b nh Kiu v tc b vi x l Kiu mng kt ni Nhm 7B_Lp 09T4 Trang 2

THC HNH CHUYN 1

Do y l c t nn cc nh sn xut thit b nh Samsung, Nokia bt buc phi thc thi y cc c t do Sun qui nh cc lp trnh vin c th da vo mi trng lp trnh nht qun v thng qua s nht qun ny, cc ng dng c to ra c th mang tnh c lp thit b cao nht c th. Hin nay Sun a ra 2 dng Configuration: CLDC (Connected Limited Device Configuration) CDC- Connected Device Configuration (Cu hnh thit b kt ni).

II. Gii php thit k v ci t chng trnh:


1. i li v chng trnh:
Ngy nay nh s tng cng h tr m thanh trong MIDP2.0, chng ta c th to nhng ng dng chi nhc trn nn Java cho nhng thit b khng dy.Chng trnh di y l mt chng trnh m phng mt Simple Playback c th chi c mt s file nhc n gin nh:midi,jts,wav.

2. Cng c thc hin.


Nhng cng c h tr v pht trin: Trnh son tho Notepad++ dng son tho chng trnh chy. Chng trnh chy dnh cho lp trnh thit b di ng: WireLess ToolKit 2.5.2. Chng trnh NetBean 6.1.

3.

Cc thnh phn trong chng trnh:

Project ng dng gm : - 2 file source(cha trong th mc \src): playaudio.java v amluong.java. - Cc file audio v image (cha trong th mc \res). - Cc file jar v jad (file chy ng dng) cha trong th mc \bin. - Cc file Class cha trong \class. Th vin cn s dng:
import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.media.*; import java.io.*;

Nhm 7B_Lp 09T4

Trang 3

THC HNH CHUYN 1


import javax.microedition.media.control.*;

Cc lp trong chng trnh: - Playaudio.java:


+class playaudio:main class public playaudio(): contructor public void startApp(): Phng thc c gi khi MIDlet c khi to v tr v trng thi tm dng. public void pauseApp(): Khi ng dng cn tm dng. public void destroyApp(boolean unconditionnal): Nhn tham s unconditional,gi khi thot MiDlet. public void commandAction(Command c, Displayable s): Thc hin vic ly danh sch(Thelist),hin tn bi ht,chy bi ht(playwavfile(songchose)) public static List getlist(): Ly danh sch cc bi ht. private void initPlayList(): To danh sch Playlist,tn chng trnh.

amluong.java:

+class amluong: public amluong(Display parent): Hin th nt Back v mute. protected void keyPressed(int keyCode): iu chnh m lng bng nt Left=true v Right=False. public void commandAction(Command c, Displayable s): Thc hin vic tt v bt m thanh. protected void paint(Graphics g): Ch ha ca chng trnh: Mu chnh:grey. Logo:Biu tng ca CT(cha trong \res). font:Italic|Bold,medium. public void playwavfile(String music): Hm chy nhc vi phng thc chnh l To ra mt Player chi nhc t InputStream createPlayer(in,ctype)(in:l stream,ctype:loi file) public void adjust(boolean m): Tng gim m lng. private static String guessContentType(String url): Tr v gi tr filetype cho bin ctype.

Nhm 7B_Lp 09T4

Trang 4

THC HNH CHUYN 1 4. Chng trnh hon chnh Playaudio.java:


import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.media.*; import java.io.*; import javax.microedition.media.control.*; public class playaudio extends MIDlet implements CommandListener { public Form f; amluong al; //danh sach bai hat static String str[]={"Noi_Buon_Hoa_Phuong.mid", "10_Nam_Tinh_Cu.mid", "Bai_Ca_Ky_Niem.mid", "Vong_Co.mid", "Gia_Tu_Di_Vang.mid","Anh_Ba_Hung.mid","Vi_yeu.mid", "Dance.mid", "A_Chang_Dep _Trai.mid", "I_want_it_that_way.mid}; public static List theList; private Command exitCommand = new Command("Exit", Command.EXIT, 1); private Command playCommand = new Command("Play", Command.ITEM, 1); private Command cm=new Command("Back",Command.BACK,1); private Command volu=new Command("Volume",Command.ITEM,1); public Display display; public String songchose; public playaudio() { display = Display.getDisplay(this); al=new amluong(display); initPlayList(); } //Phuong thuc StartApp dc goi khi MIDlet khoi tao public void startApp(){ display.setCurrent(theList); } // Phuong thuc tam dung public void pauseApp(){} public void destroyApp(boolean unconditionnal){ al=null; theList.deleteAll(); display.setCurrent(null); } //lenh cho nhan command public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(true); notifyDestroyed(); } else if (((s == theList) && (c == List.SELECT_COMMAND)) || (c == playCommand)) {

Nhm 7B_Lp 09T4

Trang 5

THC HNH CHUYN 1


int i = theList.getSelectedIndex(); songchose=theList.getString(i); display.setCurrent(al); al.playwavfile(songchose); al.msg=songchose; al.setTicker(new Ticker(songchose)); } } public static List getlist(){ return theList; } //khoi tao danh sach private void initPlayList() { theList= new List("Media Player", Choice.IMPLICIT,str,null); theList.setTicker(new Ticker("Tuyen chon mot so ca khuc dac sac")); theList.addCommand(exitCommand); theList.addCommand(playCommand); theList.setCommandListener(this); } }

amluong.java:
import import import import import javax.microedition.midlet.*; javax.microedition.lcdui.*; javax.microedition.media.*; java.io.*; javax.microedition.media.control.*;

class amluong extends Canvas implements CommandListener{ List list=new List("danh sach",Choice.IMPLICIT); Player player; static Image logo = null; Display pr; VolumeControl volume ; String msg=""; public static boolean mu=true; public static int volumesetting=100; private Command backCommand = new Command("Back", Command.BACK, 1); private Command MuteVolume = new Command("MUTE", Command.ITEM, 1); //khoi tao public amluong(Display parent) {

Nhm 7B_Lp 09T4

Trang 6

THC HNH CHUYN 1


pr=parent; this.addCommand(backCommand); this.addCommand(MuteVolume); setCommandListener(this); } protected void keyPressed(int keyCode) { int action = getGameAction(keyCode); switch (action) { case LEFT:adjust(true);break; case RIGHT:adjust(false);break; } repaint(); } public void commandAction(Command c, Displayable s) { if(c==MuteVolume){ volume.setMute(mu); if(mu==true) mu=false; else mu=true; } else if(c==backCommand){ if(player!=null) { player.close(); player=null; } pr.setCurrent(playaudio.getlist()); } } protected void paint(Graphics g) { g.setColor(9); g.fillRect(getWidth()/4,getHeight()/4, getWidth()/2, getHeight()/2); // Draw with black pen g.setColor(0xFF7f00); try { logo = Image.createImage("/logo.png"); } catch (Exception ex) { logo = null; } g.drawImage(logo,120 ,100,Graphics.TOP | Graphics.HCENTER); g.setFont(Font.getFont(Font.FACE_SYSTEM ,Font.STYLE_ITALIC,Font.SIZE_MEDIUM )); // Set message to current volume setting //if (!volumemuted) String msg1; msg1 = "Volume: " + Integer.toString(volumesetting); /*else

Nhm 7B_Lp 09T4

Trang 7

THC HNH CHUYN 1


msg = "Volume muted";*/ // Center the message on the display g.drawString(msg, getWidth()/2, getHeight()/2-50, Graphics.TOP | Graphics.HCENTER); g.drawString(msg1, getWidth()/2, (getHeight()/2)+15, Graphics.TOP | Graphics.HCENTER); } //play music public void playwavfile(String music) { try{ // Read wav file, packaged in the jar file InputStream in = getClass().getResourceAsStream("/"+music); String ctype=guessContentType(music); player = Manager.createPlayer(in,ctype); player.start(); volume=(VolumeControl)player.getControl("VolumeControl"); //player.setLoopCount(3); } catch (Exception e) { // Display an alert Alert alr = new Alert("Error", e.getMessage(), null, AlertType.ERROR); alr.setTimeout(Alert.FOREVER); pr.setCurrent(alr,this); } } // public void adjust(boolean m){ if(m==true){volumesetting = volume.getLevel();volumesetting=volumesetting-3; volumesetting = volume.setLevel(volumesetting); } else{ volumesetting = volume.getLevel();volumesetting=volumesetting+3; volumesetting = volume.setLevel(volumesetting); } } private static String guessContentType(String url) throws Exception { String ctype; // some simple test for the content type if (url.endsWith("wav")) { ctype = "audio/x-wav"; } else if (url.endsWith("jts")) {

Nhm 7B_Lp 09T4

Trang 8

THC HNH CHUYN 1


ctype } else if ctype } else if ctype } else if ctype } else { throw from URL: " + url); } } } = "audio/x-tone-seq"; (url.endsWith("mid")) { = "audio/midi"; (url.endsWith("mp3")) { = "audio/mpeg"; (url.endsWith("au")) { = "audio/basic"; new Exception("Cannot guess content type

return ctype;

Nhm 7B_Lp 09T4

Trang 9

THC HNH CHUYN 1

III. Kt qu chy DEMO

Nhm 7B_Lp 09T4

Trang 10

THC HNH CHUYN 1

LI CM N
Chng em xin chn thnh cm n thy TRNH CNG DUY dy, cung cp cho chng em nhng kin thc, ti liu tht b ch, chng c th hon thnh ti tt nht trong kh nng hin ti ca mnh. Chng em cng xin trn thnh tt c bn b gip chng em trong qu trnh lm bi tp ny. Mc d ht sc c gng khai thc tm hiu thng tin t nhiu ngun ti liu, song do bc u tip xc vi J2ME cng nh lp trnh Mobile ni chung nn phn mm ca chng em khng th trnh khi nhiu thiu st. Knh mong qu thy c gio cng nh cc bn c nhng kin ng gp chn thnh nht chng em c th dn hon thin bi lm ca mnh. Mt ln na chng em xin trn thnh cm n thy gio TRNH CNG DUY v tt c cc bn. Nhm SV thc hin: TRN THANH DUY PHAN NG MI

Nhm 7B_Lp 09T4

Trang 11

You might also like