Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Como fazer um player de Vídeo em Java ME

From Wiki
Jump to: navigation, search
Article Metadata

Artigo
Tradução:
Por maiconherverton
Última alteração feita por lpvalente em 14 Feb 2014

Este artigo mostra como criar criar uma tela de inicialização que pode reproduzir arquivos de vídeo de qualquer formato (GIF, AVI, MPG, 3GPP, real etc.)

Classe do player de vídeo

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();
// Cria a instância do player
 
player = Manager.createPlayer(is, this.MIMEtype);
 
player.prefetch();
player.addPlayerListener(this);
// inicia o player
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;
 
}
}

segundo, este é o exemplo de lançá-lo na forma de MIDelt, note que tudo o que tenho que fazer é apenas criar e iniciar a nova instância da classe videosplash

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;
}
 
}
This page was last modified on 14 February 2014, at 20:48.
231 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×