Problema MMAPI J2ME

5 respostas
guihgf_10

Boa Tarde a Todos.

Galera, segui o exemplo abaixo de um livro de mmapi e o video carrega(vejo que o meu wireless pisca muito rápido para acessar), porém a tela fica preta e não mostra nada:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

public class Video extends MIDlet implements CommandListener 
{
    // the list to show the choice - form or canvas
    private List list = null;
    // the canvas to display the video on
    private Canvas canvas = null;
    // the form to add the video to
    private Form form = null;
    // a string item to add to form
    private StringItem descriptionItem = null;
    // the video player
    Player player = null;
    // commands
    private Command backCommand = null;
    private Command exitCommand = null;

    // alert to show messages on
    private Alert alert = null;
    // and the display
    private Display display = null;
    // a flag to indicate error
    private boolean error = false;
public Video() 
{
    // create the visual elements
    display = Display.getDisplay(this);
    exitCommand = new Command("Exit", Command.EXIT, 1);
    backCommand = new Command("Back", Command.ITEM, 1);
    // VideoCanvas is a non public class in this file
    canvas = new VideoCanvas();
    canvas.addCommand(exitCommand);
    canvas.addCommand(backCommand);
    canvas.setCommandListener(this);
    // create the form and add items and commands to it
    form = new Form("Video Form", null);
    descriptionItem = new StringItem("Desc: ", "Sydney Harbour - Bad audio");
    form.append(descriptionItem);
    form.addCommand(exitCommand);
    form.addCommand(backCommand);
    form.setCommandListener(this);
    // create the list
    list = new List("Pick One", List.IMPLICIT);
    list.append("Play Video on Form", null);
    list.append("Play Video on Canvas", null);
    list.addCommand(exitCommand);
    list.setCommandListener(this);
    // and an alert for errors
    alert = new Alert("Error");
}
public void startApp() 
{
    if(error) return;
    display.setCurrent(list); // show the list if no errors
}
public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
// close the player instance
try {
if(player != null) player.close();
} catch(Exception e) {
error(e);
}
}
public void commandAction(Command cmd, Displayable disp) {
// if exit
if(cmd == exitCommand) {
destroyApp(true);
notifyDestroyed();
} else if(cmd == backCommand) { // if the user clicks back
// close the player instance
try {
if(player != null) player.close();
} catch(Exception e) {
error(e);
}
// display the list
display.setCurrent(list);
// and return
return;
}
// implicit list handling
try {
// first load the Player instance
loadPlayer();
if(list.getSelectedIndex() == 0) { // form video
// extract the GUIControl
GUIControl guiControl = (GUIControl)player.getControl("javax.microedition.media.control.GUIControl");
// if not found, throw error
if(guiControl == null) throw new Exception("No GUIControl!!");

// add as a video item by initializing it to use GUI Primitive
Item videoItem =(Item)guiControl.initDisplayMode(GUIControl.USE_GUI_PRIMITIVE,null);
// insert at first place
form.append(videoItem);
// show the form
display.setCurrent(form);
// finally start the player instance
player.start();
} else
{ // canvas video
// grab the videocontrol
VideoControl videoControl = (VideoControl)player.getControl("javax.microedition.media.control.VideoControl");
// if not found throw error
if(videoControl == null) throw new Exception("No VideoControl!!");
// initialize to use direct video and show on canvas
videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
// make sure it is displayed full screen
// not all devices support this, if your device is having trouble
// showing the video, comment this line
videoControl.setDisplayFullScreen(true);
// must make the control visible
videoControl.setVisible(true);
// now show the canvas
display.setCurrent(canvas);
// and start the player
player.start();
}
} catch(Exception e) {
error(e);
}
}
private void loadPlayer() throws Exception
{

// loads the Player on this MP4 file.
// IMPORTANT: Change content type here for C975 to video/mp4
// and M75 to video/mpeg4 or use Netbeans device fragmentation
// feature
player = Manager.createPlayer("http://java.sun.com/products/java-media/mma/media/test-mpeg.mpg");
player.realize();
}
// general purpose error method, displays onscreen as well to output
private void error(Exception e) {
alert.setString(e.getMessage());
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
e.printStackTrace();
error = true;
}
}
// VideoCanvas that is the container for the video
class VideoCanvas extends Canvas {
public void paint(Graphics g) {
// does nothing..
}
}

Alguém já passou por esse problema ou sabe oque é ?

Até mais!

5 Respostas

guihgf_10

galera já consegui, alguem sabe quantos megas o video deve ter? e que formatos são suportados ?

P

Se vc não postou sua resposta ao problema para ajudar os demais futuros que venham aqui, por quê eu deveria responder a pergunta seguinte ? :?

Post resolvido e sem resposta no fórum = Volte 2 casas e fique parado uma rodada ! :!: :cry:

guihgf_10

Opa, foi mal é a correria do dia.

Eu simplesmente coloquei o display.setCurrent(canvas); antes do videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);

Segue código correto:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

public class Video extends MIDlet implements CommandListener
{
// the list to show the choice - form or canvas
private List list = null;
// the canvas to display the video on
private Canvas canvas = null;
// the form to add the video to
private Form form = null;
// a string item to add to form
private StringItem descriptionItem = null;
// the video player
Player player = null;
// commands
private Command backCommand = null;
private Command exitCommand = null;
private Command pause = null;

// alert to show messages on
private Alert alert = null;
// and the display
private Display display = null;
// a flag to indicate error
private boolean error = false;
public Video() {
// create the visual elements
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.EXIT, 1);
backCommand = new Command("Back", Command.ITEM, 1);
// VideoCanvas is a non public class in this file
canvas = new VideoCanvas();
canvas.addCommand(exitCommand);
canvas.addCommand(backCommand);
canvas.setCommandListener(this);
// create the form and add items and commands to it
form = new Form("Video Form", null);
descriptionItem = new StringItem("Desc: ", "Sydney Harbour - Bad audio");
form.append(descriptionItem);
form.addCommand(exitCommand);
form.addCommand(backCommand);
form.setCommandListener(this);
// create the list
list = new List("Pick One", List.IMPLICIT);
list.append("Play Video on Form", null);
list.append("Play Video on Canvas", null);
list.addCommand(exitCommand);
list.setCommandListener(this);
// and an alert for errors
alert = new Alert("Error");
}
public void startApp() {
if(error) return;
display.setCurrent(list); // show the list if no errors
}
public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
// close the player instance
try {
if(player != null) player.close();
} catch(Exception e) {
error(e);
}
}
public void commandAction(Command cmd, Displayable disp) {
// if exit
if(cmd == exitCommand) {
destroyApp(true);
notifyDestroyed();
} else if(cmd == backCommand) { // if the user clicks back
// close the player instance
try {
if(player != null) player.close();
} catch(Exception e) {
error(e);
}
// display the list
display.setCurrent(list);
// and return
return;
}
// implicit list handling
try {
// first load the Player instance
loadPlayer();
if(list.getSelectedIndex() == 0) { // form video
// extract the GUIControl
GUIControl guiControl = (GUIControl)player.getControl("javax.microedition.media.control.GUIControl");
// if not found, throw error
if(guiControl == null) throw new Exception("No GUIControl!!");

// add as a video item by initializing it to use GUI Primitive
Item videoItem =(Item)guiControl.initDisplayMode(GUIControl.USE_GUI_PRIMITIVE,null);
// insert at first place
form.append(videoItem);
// show the form
display.setCurrent(form);
// finally start the player instance
player.start();
} else
{ // canvas video
// grab the videocontrol
VideoControl videoControl = (VideoControl)player.getControl("javax.microedition.media.control.VideoControl");
// if not found throw error
if(videoControl == null) throw new Exception("No VideoControl!!");
// initialize to use direct video and show on canvas
display.setCurrent(canvas);
videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
// make sure it is displayed full screen
// not all devices support this, if your device is having trouble
// showing the video, comment this line
videoControl.setDisplayFullScreen(true);
// must make the control visible
videoControl.setVisible(true);
// now show the canvas
// and start the player
player.start();
}
} catch(Exception e) {
error(e);
}
}
private void loadPlayer() throws Exception
{

// loads the Player on this MP4 file.
// IMPORTANT: Change content type here for C975 to video/mp4
// and M75 to video/mpeg4 or use Netbeans device fragmentation
// feature
player = Manager.createPlayer("http://localhost:8084/manager/mvideos/teste.mpg");
player.realize();
}
// general purpose error method, displays onscreen as well to output
private void error(Exception e) {
alert.setString(e.getMessage());
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
e.printStackTrace();
error = true;
}
}
// VideoCanvas that is the container for the video
class VideoCanvas extends Canvas {
public void paint(Graphics g) {
// does nothing..
}
}
package Forms;

import Conexao.Conexao;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.StringItem;
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.PitchControl;
import javax.microedition.media.control.TempoControl;
import javax.microedition.media.control.VideoControl;
import javax.microedition.media.control.VolumeControl;

/**
 *
 * @author Guii
 */
public class MaterialAudio extends Form implements ItemStateListener,CommandListener
{
    Player midiPlayer=null;
    VolumeControl volControl=null;
    PitchControl pitchControl=null;
    TempoControl tempoControl=null;
    Command start=null,parar=null;

    Form frmAudio=null, frmVideo=null;

    Gauge volGauge=null;
    Gauge pitchGauge=null;
    Gauge tempoGauge=null;

    String codmat;
    Display display;
    Materiais m;
    String tipo;

    DataOutputStream os=null;
    DataInputStream is=null;
    Conexao conn= new Conexao();
    String url="http://localhost:8084/MlearningServ/MateriaisAVT";


    public MaterialAudio(String tipo,Object codmat,Display display) throws IOException
    {
                super("materiAL");
                this.codmat=String.valueOf(codmat);
                this.display=display;
                this.tipo=tipo;
                System.out.println("Material é "+codmat);

                if(tipo.equals("audio"))
                {
                    Audio();
                }
    }
 
    public void itemStateChanged(Item item)
    {
        if(!(item instanceof Gauge)) return;

        Gauge gauge =(Gauge)item;
        int val=gauge.getValue();

        if(item==volGauge)
        {
            volControl.setLevel(val);
            volGauge.setLabel("Volume: "+val);
        }
        if(item==tempoGauge&&tempoControl !=null)
        {
            tempoControl.setTempo((val)*10*1000);
            tempoGauge.setLabel("Tempo: "+ (val * 10));
        }
        if(item==pitchGauge && pitchControl !=null)
        {
            pitchControl.setPitch((val-5)*12*1000);
            pitchGauge.setLabel("Pitch: "+ (val -5));
        }
    }

    public void commandAction(Command c, Displayable d) {
        if(c==start)
        {
            try {
                Play();
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (MediaException ex) {
                ex.printStackTrace();
            }

        }
        else if(c==parar)
        {
             try {
                try {
                    conn.Close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                 midiPlayer.stop();

            } catch (MediaException ex) {
                ex.printStackTrace();
            }
        }
    }
    public String Video() throws IOException
    {

        start=new Command("Play",Command.ITEM, 1);
        frmVideo = new Form("Playing Video...");
        frmVideo.addCommand(start);

        conn.run(url);
      os=conn.Output();

      os.writeUTF("Audio");
      os.writeUTF(codmat);

      is=conn.Input();

      String nome=is.readUTF();
      String link=is.readUTF();

      return link;

    }
    public String Audio() throws IOException
    {

       frmAudio= new Form("teste");
       display.setCurrent(frmAudio);
       volGauge=new Gauge("Volume: 50", true, 100, 50);
       tempoGauge=new Gauge("Tempo: 120", true, 30, 12);
        pitchGauge=new Gauge("Pitch:0", true, 10, 5);

        start=new Command("Play",Command.ITEM, 1);
        parar = new Command("Parar",Command.ITEM, 2);


        frmAudio.append(volGauge);
        frmAudio.append(tempoGauge);
        frmAudio.append(pitchGauge);
        frmAudio.addCommand(start);
        frmAudio.addCommand(parar);

        frmAudio.setItemStateListener(this);
        frmAudio.setCommandListener(this);

      conn.run(url);
      os=conn.Output();

      os.writeUTF("Audio");
      os.writeUTF(codmat);

      System.out.println("o codmat é "+codmat);
      os.close();

      is=conn.Input();

      String nome=is.readUTF();
      String link=is.readUTF();

      return link;
    }
    public void Play() throws IOException, MediaException
    {
      String link=null;
      if(tipo.equals("audio"))
      {
          link=Audio();
      }
      else if(tipo.equals("videos"))
      {
          link=Video();
      }
      
      midiPlayer = Manager.createPlayer("http://localhost:8084/manager/"+link);
      midiPlayer.prefetch();

      /*if(tipo.equals("video"))
      {
         link=Video();
         midiPlayer = Manager.createPlayer("http://localhost:8084/manager/"+link);
         //midiPlayer.addPlayerListener(this);
         // realize the player
         midiPlayer.realize();
         VideoControl vc = (VideoControl)midiPlayer.getControl("VideoControl");
        if(vc != null)
        {
            Item video = (Item)vc.initDisplayMode(vc.USE_GUI_PRIMITIVE, null);

            StringItem si = new StringItem("Status: ", "Playing...");
            frmVideo.append(si);
            frmVideo.append(video);
            display.setCurrent(frmVideo);
        }
      }*/
        volControl=(VolumeControl)midiPlayer.getControl("javax.microedition.media.control.VolumeControl");
        pitchControl=(PitchControl)midiPlayer.getControl("javax.microedition.media.control.PitchControl");
        tempoControl=(TempoControl)midiPlayer.getControl("javax.microedition.media.control.TempoControl");
      try
      {
        midiPlayer.start();
        conn.Close();
      }
             catch (MediaException ex) {
                ex.printStackTrace();
            }
    }

}

É bom pra fica aqui registrado, pq foi bem tenso de faze e buscar essas informações, valeu pelo toque boone.

Até mais!

guihgf_10

O segundo é audio porém estende de form, mas o conceito é o mesmo.

guihgf_10

Alguem sabe qual é o tamanho maximo que o video pode ter ? Preciso roda no maximo um video de 5 min. Acho que ele suporta até 2 mb mas não consigo baixar para esse tamanho

Criado 13 de outubro de 2010
Ultima resposta 17 de out. de 2010
Respostas 5
Participantes 2