创建视频启动画面的Java实现

Java编程语言中,创建一个启动画面,特别是视频启动画面,可以显著提升应用程序的用户体验。本文将介绍如何使用Java实现一个简单的视频启动画面,该画面可以播放GIF、AVI、MPG、3GPP、Real等格式的视频文件。

视频启动画面类

首先,需要创建一个名为Splash的类,该类继承自Canvas并实现了PlayerListener和Runnable接口。这个类将负责播放视频文件,并在视频播放结束后跳转到下一个屏幕。

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; private Displayable next; 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(); } 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); } 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) { if (player.getState() == Player.STARTED) { player.stop(); } if (player.getState() == Player.PREFETCHED) { player.deallocate(); } if (player.getState() == Player.REALIZED || player.getState() == Player.UNREALIZED) { player.close(); } } player = null; } private void play(String url) { try { InputStream is = getClass().getResourceAsStream(url); VideoControl vc; resetplayer(); player = Manager.createPlayer(is, this.MIMEtype); player.prefetch(); player.addPlayerListener(this); player.realize(); vc = (VideoControl) player.getControl("VideoControl"); 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.prefetch(); player.start(); this.display.setCurrent(this); } catch (Throwable t) { player = null; nextScreen(); } } private void stopPlayer() { try { resetplayer(); } catch (MediaException me) { } player = null; } }

上述代码定义了一个Splash类,它初始化视频播放器,并在视频播放结束后跳转到下一个屏幕。

从MIDlet启动视频启动画面

接下来,需要从MIDlet中启动Splash类。以下是SplashMIDlet类的示例代码,它创建并初始化Splash类的实例。

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 display = Display.getDisplay(this); Splash sp = new Splash(display, 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; } }
沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485