课外天地 李树青学习天地JavaME移动开发课件 → 程序代码——支持暂停和继续的WAV音频播放


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

主题:程序代码——支持暂停和继续的WAV音频播放

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


加好友 发短信 管理员
等级:管理员 帖子:1949 积分:26803 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——支持暂停和继续的WAV音频播放  发帖心情 Post By:2008/10/14 20:56:08 [显示全部帖子]

import java.io.InputStream;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.midlet.MIDlet;

public class Exec extends MIDlet {
        private Display display;
        DrawPanel dp = new DrawPanel();

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

        public void startApp() {
                display.setCurrent(dp);
        }

        public void pauseApp() {
        }

        public void destroyApp(boolean unconditional) {
        }
}

class DrawPanel extends Canvas {
        InputStream in = null;
        Player p = null;
        int length;
        int current = 0;
        Thread thread = new Thread(new PlayerUpdater());
        boolean flag = false;

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

                length = (int) (p.getDuration() / 1000 / 1000);
                thread.start();
        }

        protected void paint(Graphics g) {
                int width = getWidth();
                int height = getHeight();
                g.setColor(255, 255, 255);
                g.fillRect(0, 0, width, height);
                g.setColor(0, 0, 0);
                g.fillRect(width / 4, height / 4,
                                (int) (width / 2 * current / (double) length), 20);
        }

        // 新增一个线程处理进度条,定义一个内部类
        class PlayerUpdater implements Runnable {
                public void run() {
                        try {
                                while (true) {
                                        if (flag) {
                                                Thread.sleep(1000);
                                                current++;
                                                repaint();
                                                if (current >= length)
                                                        break;
                                        }
                                }
                        } catch (Exception e) {
                                System.out.println(e.getMessage());
                        }
                }
        }

        protected void showNotify() {
                flag = true;
                try {
                        p.prefetch();
                        p.setMediaTime(current * 1000 * 1000L);
                        p.start();
                } catch (Exception e) {
                        System.out.println(e.getMessage());
                }
        }

        protected void hideNotify() {
                try {
                        p.stop();
                } catch (Exception e) {
                        System.out.println(e.getMessage());
                }
                flag = false;
        }
}

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

 回到顶部