⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 videocanvas.java

📁 索爱的多媒体例程,包括播放音乐,视频等
💻 JAVA
字号:
/* * @(#)VideoCanvas.java	1.5 03/02/27 * * Copyright (c) 2000-2003 Sun Microsystems, Inc. All rights reserved.  * PROPRIETARY/CONFIDENTIAL * Use is subject to license terms */package example.mmademo;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.media.*;import javax.microedition.media.control.*;import java.io.*;/** * Play Video in a Canvas * */public class VideoCanvas extends Canvas    implements Runnable, CommandListener, PlayerListener {        private static String         TITLE_TEXT = "MMAPI Player";    static Player player = null;        int idx = 0;    boolean fsmode = false;    Display parentDisplay;    long duration;    long lastTime = -1;    Thread sliderThread;    private Command     backCommand       = new Command("Back",                                                        Command.BACK, 1);    private Command     playCommand       = new Command("Play",                                                        Command.ITEM, 1);    private Command     snapCommand       = new Command("Snapshot",                                                        Command.ITEM, 1);    private Command     pauseCommand       = new Command("Pause",                                                         Command.ITEM, 10);    VolumeControl vc;    RateControl rc;    VideoControl vidc;    FramePositioningControl fpc;    private int CB_Y = 136;    private int CB_H = 8;    private int TH_W = 6;    private boolean vis = true;        public VideoCanvas(Display parentDisplay) {        this.idx = 0;        this.parentDisplay = parentDisplay;        initialize();    }    public void paint(Graphics g) {        // Only video player uses the screen in fullscreen mode        if (fsmode) return;        // Draw the GUI and media time slider        g.setColor(0x9090E0);        g.fillRect(0, 0, 180, 180);        g.setColor(0x202050);        //Video Border        g.drawLine(9, 7, 160+9, 7);        g.drawLine(9, 7, 9, 120+7);        //Control Bar        if (sliderThread != null) {            g.drawLine(9, CB_Y, 160+9, CB_Y);            g.drawLine(9, CB_Y, 9, CB_Y+CB_H);        }        g.setColor(0xD0D0FF);        //Video Border        g.drawLine(160+10, 8, 160+10, 8+120);        g.drawLine(160+10, 120+8, 10, 120+8);        //Control Bar        if (sliderThread != null) {            g.drawLine(160+10, CB_Y+1, 160+10, CB_Y+CB_H+1);            g.drawLine(160+10, CB_Y+CB_H+1,10, CB_Y+CB_H+1);        }        if (sliderThread != null) {            int p = time2pix(lastTime);            g.drawLine(10+p, CB_Y+1, 10+p, CB_Y+CB_H);            g.drawLine(10+p, CB_Y+1, 10+p+TH_W-1, CB_Y+1);            g.setColor(0x202050);            g.drawLine(10+p+1, CB_Y+CB_H, 10+p+TH_W, CB_Y+CB_H);            g.drawLine(10+p+TH_W, CB_Y+CB_H, 10+p+TH_W, CB_Y+1);            g.setColor(0xA0A0FF);            g.fillRect(10+p+1, CB_Y+2, TH_W-2, CB_H-2);            g.setColor(0);            g.drawString("00:00", 10, CB_Y+CB_H+4, Graphics.TOP | Graphics.LEFT);            g.drawString(time2String(lastTime) + "/" + time2String(duration),                         10+160, CB_Y+CB_H+4,                         Graphics.TOP | Graphics.RIGHT);        }    }    void initialize() {        addCommand(backCommand);        addCommand(snapCommand);        setCommandListener(this);    }    private int time2pix(long time) {        int t2p = (int) ((time * (159-TH_W))/duration);        return t2p;    }    private String time2String(long time) {        time = time / 1000000;        String strTime = "" + (time % 10);        time = time / 10;        strTime = (time % 6) + strTime;        time = time / 6;        strTime = (time % 10) + ":" + strTime;        time = time / 10;        strTime = (time % 6) + strTime;        time = time / 6;        return strTime;    }    public void run() {        while (player != null) {            try {                long time = player.getMediaTime();                if (time2pix(time) != time2pix(lastTime)) {                    lastTime = time;                    repaint(8, CB_Y, 162, CB_H+2+20);                }                Thread.sleep(250);            } catch (Exception e) {                break;            }            }    }    /*     * Respond to commands, including back     */    public void commandAction(Command c, Displayable s) {        if (s == this) {            if (c == backCommand) {                close();                parentDisplay.setCurrent(VideoTest.getList());            } else if (vidc != null && c == snapCommand) {                doSnapshot();            } else if (vidc == null && c == pauseCommand) {                removeCommand(pauseCommand);                addCommand(playCommand);                pause();            } else if (vidc == null && c == playCommand) {                start();                removeCommand(playCommand);                addCommand(pauseCommand);            }        }    }    /*     * Process clicking in slider region     */    protected void pointerPressed(int x, int y) {        if (duration == Player.TIME_UNKNOWN)            return;        if (y >= CB_Y && y < CB_Y + CB_H) {            if (x >= 8 && x <= 8 + 160) {                x = x - 11;                if (x < 0) x = 0;                if (x > 160 - TH_W)                    x = 160 - TH_W;                long time = (duration * x) / (160 - TH_W);                try {                    player.setMediaTime(time);                } catch (Throwable t) {                }            }        }    }    protected void pointerDragged(int x, int y) {        pointerPressed(x, y);    }        public void open(String url) {        try {            synchronized (this) {                if ( player == null ) {                    if (url.startsWith("resource:")) {                        InputStream ins = getClass().getResourceAsStream(url.substring(9));                        player = Manager.createPlayer(ins, "video/mpeg");                    } else {                        player = Manager.createPlayer(url);                    }                    player.addPlayerListener(this);                }            }            player.realize();            if ((vidc = (VideoControl) player.getControl("VideoControl")) != null) {                vidc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);                vidc.setDisplayLocation(10, 8);                vidc.setDisplaySize(160, 120);                vidc.setVisible(true);            }            Control [] controls = player.getControls();                        for (int i = 0; i < controls.length; i++) {                if (controls[i] instanceof VolumeControl)                    vc = (VolumeControl) controls[i];                if (controls[i] instanceof RateControl)                    rc = (RateControl) controls[i];                if (controls[i] instanceof FramePositioningControl)                    fpc = (FramePositioningControl) controls[i];            }            player.prefetch();            if (vidc == null)                addCommand(pauseCommand);        } catch (Exception me) {            player = null;        }    }    public void start() {        if (player == null)            return;        try {            duration = player.getDuration();            player.start();            if (duration != Player.TIME_UNKNOWN) {                sliderThread = new Thread(this);                sliderThread.start();            }        } catch (Exception ex) {            player = null;        }    }    public void close() {        synchronized (this) {            pause();            if (player != null) {                player.close();                player = null;            }        }        VideoTest.getInstance().nullPlayer();    }    public void pause() {        if ( player != null) {            try {                player.stop();            } catch (MediaException e) {}        }    }    public void playerUpdate(Player plyr, String evt, Object evtData) {        if ( evt == END_OF_MEDIA ) {            try {                player.setMediaTime(0);                player.start();            } catch (MediaException ex) {}        }    }    private void doSnapshot() {        try {            byte [] snap = vidc.getSnapshot("encoding=png");            if (snap != null) {            Image im = Image.createImage(snap, 0, snap.length);            Alert al = new Alert("Snapshot",                         "Here's the snap",                         im,                         AlertType.INFO);            al.setTimeout(2000);            parentDisplay.setCurrent(al, this);            }        } catch (MediaException me) {            System.err.println(me);        }    }        public synchronized void stopVideoCanvas() {        player.deallocate();    }    /* Handle the different keys pressed on the phone GUI emulator */    public void keyPressed(int keyCode) {        int cr, cv;        switch (keyCode) {        case Canvas.KEY_NUM4:            cr = rc.getRate();            cr -= 10000;            cr = rc.setRate(cr);            break;        case Canvas.KEY_NUM6:            cr = rc.getRate();            cr += 10000;            cr = rc.setRate(cr);            break;        case Canvas.KEY_STAR:            if (vc != null) {                cv = vc.getLevel();                cv -= 10;                cv = vc.setLevel(cv);            }            break;        case Canvas.KEY_NUM0:            if (vc != null) {                vc.setMute(!vc.isMuted());            }            break;        case Canvas.KEY_POUND:            if (vc != null) {                cv = vc.getLevel();                cv += 10;                cv = vc.setLevel(cv);            }            break;        case Canvas.KEY_NUM7:            if (fpc != null) {                fpc.skip(-1);            }            break;        case Canvas.KEY_NUM5:            try {                player.stop();                player.setMediaTime(0);                player.deallocate();            } catch (MediaException me) {            }            break;        case Canvas.KEY_NUM9:            if (fpc != null) {                fpc.skip(1);            }            break;        case Canvas.KEY_NUM2:            try {                if (player.getState() == Player.STARTED)                    player.stop();                else                    player.start();            } catch (Exception e) {}            break;        case Canvas.KEY_NUM8:            try {                // Full screen                if (vidc != null)                    vidc.setDisplayFullScreen(fsmode = !fsmode);                repaint();            } catch (MediaException me) {}                        break;        case Canvas.KEY_NUM1:        case Canvas.KEY_NUM3:            {                long mTime = player.getMediaTime();                long duration = player.getDuration();                if (duration == Player.TIME_UNKNOWN || mTime == Player.TIME_UNKNOWN)                    return;                try {                    if (keyCode == Canvas.KEY_NUM3) {                        // Jump forward 10%                        mTime += duration / 10;                        if (mTime > duration)                            mTime = duration;                        player.setMediaTime(mTime);                    } else if (keyCode == Canvas.KEY_NUM1) {                        mTime -= duration / 10;                        if (mTime < 0)                            mTime = 0;                        player.setMediaTime(mTime);                    } else                        return;                } catch (MediaException me) {                }            }            break;            /* Code to move the video around using cursor keys */            default:            int game = getGameAction(keyCode);            int x = vidc.getDisplayX();            int y = vidc.getDisplayY();            if (game == UP)                vidc.setDisplayLocation(x, y - 10);            else if (game == DOWN)                vidc.setDisplayLocation(x, y + 10);            else if (game == LEFT)                vidc.setDisplayLocation(x - 10, y);            else if (game == RIGHT)                vidc.setDisplayLocation(x + 10, y);            else if (game == FIRE)                vidc.setVisible(vis = !vis);            repaint();            break;                    }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -