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

📄 videocanvas.java

📁 MAPI手机多媒体播放的例子
💻 JAVA
字号:
package com.nokia.example.mmapi.mediasampler.viewer;

import java.io.*;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

import com.nokia.example.mmapi.mediasampler.MediaSamplerMIDlet;

/**
 * VideoCanvas renders video on Canvas.
 */
class VideoCanvas extends Canvas implements CommandListener, PlayerListener
{
    private PlayerController controller;

    private MediaSamplerMIDlet midlet;

    private Displayable returnScreen;

    private String videoFile;

    private Command stopCommand;

    private Command replayCommand;

    private Command backCommand;

    private Player player;

    private boolean initDone;

    private boolean playPending = false;

    /**
     * Constructor.
     * 
     * @param midlet
     *            MediaSamplerMIDlet
     * @param returnScreen
     *            Displayable to set visible when returned from this Canvas
     * @param videoFile
     *            String as path of the source viudeo file.
     */
    VideoCanvas(MediaSamplerMIDlet midlet, Displayable returnScreen,
            String videoFile)
    {
        this.midlet = midlet;
        this.returnScreen = returnScreen;
        this.videoFile = videoFile;
        controller = new PlayerController();

        replayCommand = new Command("Replay", Command.SCREEN, 1);
        stopCommand = new Command("Stop", Command.SCREEN, 2);
        backCommand = new Command("Back", Command.BACK, 1);
        addCommand(backCommand);
        setCommandListener(this);
    }

    /**
     * Set play status to "pending".
     */
    void prepareToPlay()
    {
        controller.start();
        playPending = true;
    }

    /**
     * Play video only when we're displayed. Use playPending flag to avoid
     * restarting if a system screen is visiable.
     */
    public void showNotify()
    {
        if (playPending)
        {
            playPending = false;
            controller.playVideo();
        }
    }

    /**
     * Renders the Canvas.
     */
    public void paint(Graphics g)
    {
        g.setColor(0x00FFFF00); // yellow
        g.fillRect(0, 0, getWidth(), getHeight());
    }

    /**
     * Implemented CommandListener method. Indicates that a command event has
     * occurred on Displayable d.
     */
    public void commandAction(Command c, Displayable d)
    {
        if (c == stopCommand)
        {
            controller.stopVideo();
        }
        else if (c == replayCommand)
        {
            controller.playVideo();
        }
        else if (c == backCommand)
        {
            discardPlayer();
        }
    }

    /**
     * Overriden Canvas method.
     */
    public void keyPressed(int keyCode)
    {
        if (getGameAction(keyCode) == FIRE)
        {
            int state = player.getState();
            if (state == Player.STARTED)
            {
                controller.stopVideo();
            }
            else
            {
                controller.playVideo();
            }
        }
    }

    /**
     * Reads the content from the specified HTTP URL and returns InputStream
     * where the contents are read.
     * 
     * @return InputStream
     * @throws IOException
     */
    private InputStream urlToStream(String url) throws IOException
    {
        // Open connection to the http url...
        HttpConnection connection = (HttpConnection) Connector.open(url);
        DataInputStream dataIn = connection.openDataInputStream();
        byte[] buffer = new byte[1000];
        int read = -1;
        // Read the content from url.
        ByteArrayOutputStream byteout = new ByteArrayOutputStream();
        while ((read = dataIn.read(buffer)) >= 0)
        {
            byteout.write(buffer, 0, read);
        }
        dataIn.close();
        connection.close();
        // Fill InputStream to return with content read from the URL.
        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteout
                .toByteArray());
        return byteIn;
    }

    /**
     * Stops the Player.
     */
    void doStop()
    {
        if (player != null)
        {
            try
            {
                player.stop();
            }
            catch (MediaException e)
            {
                e.printStackTrace();
            }
        }
    }

    /**
     * Initializes and starts the Player.
     */
    void doPlay()
    {
        try
        {
            if (!initDone || player == null)
            {
                initPlayer();
            }

            int state = player.getState();
            if (state == Player.CLOSED)
            {
                player.prefetch();
            }
            else if (state == Player.UNREALIZED)
            {
                player.realize();

            }
            else if (state == Player.REALIZED)
            {
                player.prefetch();
            }

            player.start();
        }
        catch (MediaException me)
        {
            discardPlayer();
            midlet.alertError("MediaException: " + me.getMessage());
        }
        catch (SecurityException se)
        {
            discardPlayer();
            midlet.alertError("SecurityException: " + se.getMessage());
        }
        catch (Exception e)
        {
            discardPlayer();
            midlet.alertError("Exception: " + e.getMessage());
        }
    }

    /**
     * Initializes the video player.
     * 
     * Player is initialized only once to save the memory resorces and to
     * increase performance.
     */
    void initPlayer()
    {
        try
        {
            initDone = false;
            if (videoFile == null)
            {
                midlet.alertError("No video file specified");
                return;
            }
            boolean fromHttp = videoFile.startsWith("http://");

            InputStream is = fromHttp ? urlToStream(videoFile) : getClass()
                    .getResourceAsStream(videoFile);
            player = Manager.createPlayer(is, "video/3gpp");
            player.addPlayerListener(this);
            player.prefetch();
            player.realize();

            // get the video control and attach it to our canvas
            VideoControl videoControl = (VideoControl) (player
                    .getControl("VideoControl"));
            if (videoControl == null)
            {
                midlet.alertError("VideoControl not supported");
            }
            else
            {
                videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO,
                        this);
                videoControl.setVisible(true);
            }
            
            initDone = true;
        }
        catch (IOException ioe)
        {
            discardPlayer();
            midlet.alertError("IOException: " + ioe.getMessage());
        }
        catch (MediaException me)
        {
            discardPlayer();
            midlet.alertError("MediaException: " + me.getMessage());
        }
        catch (SecurityException se)
        {
            discardPlayer();
            midlet.alertError("SecurityException: " + se.getMessage());
        }
        catch (Exception ex)
        {
            discardPlayer();
            midlet.alertError("Exception: " + ex.getMessage());
        }

    }

    /**
     * Called in case of exception to make sure invalid players are closed
     */
    void discardPlayer()
    {
        if (player != null)
        {
            controller.setStopped();
            player.close();
            player = null;
            Display.getDisplay(midlet).setCurrent(returnScreen);
        }
    }

    /**
     * Implemented javax.microedition.media.PlayerListener method.
     */
    public void playerUpdate(final Player p, final String event,
            final Object eventData)
    {
        // queue a call to updateEvent in the user interface event queue
        Display display = Display.getDisplay(midlet);
        display.callSerially(new Runnable()
        {
            public void run()
            {
                VideoCanvas.this.updateEvent(p, event, eventData);
            }
        });
    }

    /**
     * Handles playerUpdate events of the Player.
     * 
     * @param p
     * @param event
     * @param eventData
     */
    void updateEvent(Player p, String event, Object eventData)
    {
        if (event == END_OF_MEDIA)
        {
            addCommand(replayCommand);
            removeCommand(stopCommand);
        }
        else if (event == CLOSED)
        {
            addCommand(replayCommand);
            removeCommand(stopCommand);
        }
        else if (event == STARTED)
        {
            removeCommand(replayCommand);
            addCommand(stopCommand);
        }
        else if (event.equals(ERROR))
        {
        }
    }

    /**
     * PlayerController calls the play and stop methods of the Player. The
     * purpose of this class is only to isolate Player method calls from the
     * event threads (such commandAction(...))
     */
    public class PlayerController extends Thread
    {
        private boolean running;

        // Lock object ofthis Thread
        private Object controllLock = new Object();

        public PlayerController()
        {
        }

        // Activates the Player
        public void playVideo()
        {
            synchronized (controllLock)
            {
                controllLock.notify();
            }
        }

        // Deactivates the player
        public void stopVideo()
        {
            synchronized (controllLock)
            {
                doStop();
            }
        }

        // Terminates this thread
        public void setStopped()
        {
            running = false;
            synchronized (controllLock)
            {
                controllLock.notify();
            }
        }

        public void run()
        {
            VideoCanvas.this.doPlay();

            running = true;
            while (running)
            {
                try
                {
                    synchronized (controllLock)
                    {
                        // Set this thread to wait for playVideo() method call.
                        controllLock.wait();
                        if (!running) // Leave if controller is stopped.
                        {
                            break;
                        }
                        VideoCanvas.this.doPlay();
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
}

⌨️ 快捷键说明

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