`
javastder
  • 浏览: 11801 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

How to make Video splash

阅读更多

http://wiki.forum.nokia.com/index.php/How_to_make_Video_splash

 

In those a few lines you will find a code of how to create easy startup splash screen that can play video files of any format (GIF,AVI,mpg,3gpp,real etc..)

video splash class

package GALAXY.videosplash;
 
import java.io.*;
 
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
 
public class Splash extends Canvas implements PlayerListener, Runnable {
    private Display display;
    //the next screen
    private Displayable next;
    //the video mime type
    private String MIMEtype;
    private Player player;
    private String file;
 
    public Splash(Display display, Displayable next, String file,
                  String MIMEtype) {
 
        this.display = display;
        this.next = next;
        this.file = file;
        this.MIMEtype = MIMEtype;
        Thread th = new Thread(this);
        th.start();
 
    }
 
    //if any button is pressed, finalizes splash screen
    protected void keyPressed(int keyCode) {
        stopPlayer();
        nextScreen();
    }
 
    protected void paint(Graphics g) {
        int x = g.getClipX();
        int y = g.getClipY();
 
        int w = g.getClipWidth();
        int h = g.getClipHeight();
 
        g.setColor(0x0000000);
        g.fillRect(x, y, w, h);
 
 
    }
 
    //for touch screen devices.
    protected void pointerPressed(int x, int y) {
        stopPlayer();
        nextScreen();
    }
 
    protected void showNotify() {
 
    }
 
    private void nextScreen() {
 
        this.display.setCurrent(next);
    }
 
    public void run() {
        try {
            resetplayer();
        } catch (MediaException ex) {
            nextScreen();
        }
        this.play(file);
 
    }
 
    public void playerUpdate(Player player, String playerstate, Object object) {
        if (playerstate == PlayerListener.END_OF_MEDIA) {
            try {
                resetplayer();
            } catch (MediaException me) {
 
            }
            player = null;
            nextScreen();
        }
 
    }
 
    private void resetplayer() throws MediaException {
        if (player != null) {
            //checks if the video is being rendered.
            if (player.getState() == Player.STARTED) {
                player.stop();
            }
            //the player has all resources to execute
            if (player.getState() == Player.PREFETCHED) {
                //free resources
                player.deallocate();
            }
            //the player obtained the necessary information to acquire the resources (REALIZED) or the player 
            //has just been created.
            if (player.getState() == Player.REALIZED ||
                player.getState() == Player.UNREALIZED) {
                player.close(); //closes the player
            }
        }
        player = null;
    }
 
    private void play(String url) {
        try {
            //gets video from /res folder
            InputStream is = getClass().getResourceAsStream(url);
            VideoControl vc;
            resetplayer();
            // create a player instance
 
            player = Manager.createPlayer(is, this.MIMEtype);
 
            // realize the player
            player.realize();
            //gets all necessary resources
            player.prefetch();
            //adds a player listener. annonces player events: stoped, paused, etc
            player.addPlayerListener(this);
 
            //video control for video rendering
            vc = (VideoControl) player.getControl("VideoControl");
            //if null, the device probably has no support for video rendering 
            if (vc != null) {
 
               vc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
               vc.setDisplayLocation(((this.getWidth() - vc.getDisplayWidth()) /
                                   2),
                                    (this.getHeight() - vc.getDisplayHeight()) /
                                   2);
 
 
                vc.setVisible(true);
 
                this.setFullScreenMode(true);
            }
            player.start();
            this.display.setCurrent(this);
        } catch (Throwable t) {
 
            player = null;
            nextScreen();
        }
    }
 
    private void stopPlayer() {
        try {
            resetplayer();
        } catch (MediaException me) {
 
        }
        player = null;
 
    }
}

second ,this is the MIDlet example. Note that all I have to do was just to create a new instance of videosplash class

MIDLET

{
package GALAXY.videosplash;
 
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
 
public class SplashMIDlet extends MIDlet {
    static SplashMIDlet instance;
 
    public SplashMIDlet() {
        instance = this;
    }
 
    public void startApp() {
        Display dispaly = Display.getDisplay(this);
 
 
        Splash sp = new Splash(dispaly, new Form("Test"),"/PhotoStory.3gp", "video/3gpp");
 
 
    }
 
    public void pauseApp() {
    }
 
    public void destroyApp(boolean unconditional) {
    }
 
    public static void quitApp() {
        instance.destroyApp(true);
        instance.notifyDestroyed();
        instance = null;
    }
 
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics