以文本方式查看主题

-  课外天地 李树青  (http://www.njcie.com/bbs/index.asp)
--  JavaME移动开发课件  (http://www.njcie.com/bbs/list.asp?boardid=18)
----  程序代码——显示播放音乐进度的Gauge  (http://www.njcie.com/bbs/dispbbs.asp?boardid=18&id=550)

--  作者:admin
--  发布时间:2008/9/27 20:13:56
--  程序代码——显示播放音乐进度的Gauge

import java.io.InputStream;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.midlet.MIDlet;

public class Exec extends MIDlet implements CommandListener {
        Display display;
        Form form = new Form("表单");
        Gauge gauge;
        Command start = new Command("开始", Command.OK, 1);
        InputStream in = null;
        Player p = null;

        public Exec() {
                display = Display.getDisplay(this);
        }

        public void startApp() {
                try {
                        in = getClass().getResourceAsStream("/musics/music.wav");
                        p = Manager.createPlayer(in, "audio/x-wav");
                } catch (Exception e) {
                        System.out.println(e.getMessage());
                }

                gauge = new Gauge("带有进度条的音乐播放MIDLET", false, 10, 0);
                form.append(gauge);
                form.addCommand(start);
                form.setCommandListener(this);
                display.setCurrent(form);
        }

        public void commandAction(Command c, Displayable s) {
                try {
                        p.start();
                        gauge.setMaxValue((int) (p.getDuration() / 1000 / 1000));
                        Thread thread = new Thread(new GaugeUpdater());
                        thread.start();
                } catch (Exception e) {
                        System.out.println(e.getMessage());
                }
        }

        public void pauseApp() {
        }

        public void destroyApp(boolean unconditional) {
        }

        // 新增一个线程处理进度条,定义一个内部类
        class GaugeUpdater implements Runnable {
                GaugeUpdater() {
                }

                public void run() {
                        try {
                                while (gauge.getValue() < gauge.getMaxValue()) {
                                        Thread.sleep(1000);
                                        gauge.setValue(gauge.getValue() + 1);
                                }
                                gauge.setLabel("结束");
                        } catch (Exception e) {
                                System.out.println(e.getMessage());
                        }
                }
        }
}

[此贴子已经被作者于2010-12-12 18:33:04编辑过]