import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VideoControl;
/** Acquires the video content and renders it */
public class VideoCanvas extends Canvas implements CommandListener, PlayerListener, Runnable {
private Display display;
private Player player;
private VideoControl videoControl;
private String url;
private Thread initializer;
private Command close;
private Command rePlay;
public VideoCanvas(){
super();
display = Display.getDisplay(PowerVideo.p);
close = new Command("close", Command.SCREEN, 1);
addCommand(close);
setCommandListener(this);
this.setFullScreenMode(true);
}
public void initializeVideo(String url){
this.url = url;
initializer = new Thread(this);
initializer.start();
this.setFullScreenMode(true);
}
public void run(){
try {
player = Manager.createPlayer(url);
player.addPlayerListener(this);
player.realize();
player.prefetch();
this.setFullScreenMode(true);
} catch (IOException ioe) {
Alert alert = new Alert("IOException thrown", ioe.getMessage(), null, AlertType.ERROR);
display.setCurrent(alert);
} catch (MediaException me) {
Alert alert = new Alert("MediaException thrown", me.getMessage(), null, AlertType.ERROR);
display.setCurrent(alert);
}
playVideo();
}
public void playVideo(){
try {
// Get the video control and set it to the current display.
videoControl = (VideoControl)player.getControl("VideoControl");
if (videoControl != null) {
videoControl.initDisplayMode(videoControl.USE_DIRECT_VIDEO, this);
}
int cHeight = this.getHeight();
int cWidth = this.getWidth();
videoControl.setDisplaySize(cWidth, cHeight);
display.setCurrent(this);
videoControl.setVisible(true);
player.start();
} catch (MediaException me) {
Alert alert = new Alert("MediaException thrown", me.getMessage(), null, AlertType.ERROR);
display.setCurrent(alert);
}
}
/** Paints background color */
public void paint(Graphics g){
g.setColor(128, 128, 128);
g.fillRect(0, 0, getWidth(), getHeight());
}
public void playerUpdate(Player p, String event, Object eventData) {
//add "Replay" option when video is finished
if (event == PlayerListener.END_OF_MEDIA)
{
if (rePlay == null)
{
rePlay = new Command("re-play", Command.SCREEN, 1);
addCommand(rePlay);
}
}
}
public void commandAction(Command c, Displayable s) {
if(c == rePlay){
try{
player.start();
} catch (MediaException me) {
Alert alert = new Alert("MediaException thrown", me.getMessage(), null, AlertType.ERROR);
display.setCurrent(alert);
}
}
else if(c == close){
player.close();
System.gc();
new FileController().showCurrentDir();
url=null;
}
}
}
J2ME 本地视频播放器
4星 · 超过85%的资源 需积分: 10 102 浏览量
2008-11-02
12:03:45
上传
评论 1
收藏 42KB RAR 举报
imagoodboy2005
- 粉丝: 13
- 资源: 6
最新资源
- 基于python实现的基于PyQt5和爬虫的小说阅读系统.zip
- 机械设计整经机上纱自动化sw20非常好的设计图纸100%好用.zip
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈