课外天地 李树青学习天地JavaME移动开发课件 → 程序代码——显示播放音乐进度的Gauge


  共有13891人关注过本帖树形打印复制链接

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

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1940 积分:26616 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——显示播放音乐进度的Gauge  发帖心情 Post By: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编辑过]

 回到顶部