Rss & SiteMap

课外天地 李树青 http://www.njcie.com

李树青 论坛 南京 财经 课外天地
共1 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

标题:程序代码——显示播放音乐进度的Gauge

1楼
admin 发表于:2008/9/27 20:13:56

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编辑过]
共1 条记录, 每页显示 10 条, 页签: [1]

Copyright ©2002 - 2016 课外天地.Net
Powered By Dvbbs Version 8.3.0
Processed in .03125 s, 2 queries.