You are on page 1of 8

Archivo: /home/oscar/NetBeansProjects/…heco/src/podcast/RepMP3_Clases Página 1 de 8

/*
Bueno me dicen bego y bueno no es la gran cosa, pero aquí les dejo unas clases de JME para reproducir
MP3
y asociarle una imagen, lo pueden usar para hacer un reproductor personalizado o incluso historietas.

Trate de hacer una implementación que corriera en PC también pero ya saben algo de trabajo y decidía.

El proyecto esta hecho en Netbeans y pues es la recopilación de algunos códigos de la red.

Espero les ayude.


*/

// ************************************* Clase fabrica ************************************

package podcast;

import javax.microedition.lcdui.ImageItem;

public class fabrica {

private static Reproductor MiReproductor;

public static Reproductor getMiReproductor( String tipo, String cades [][], int index, ImageItem II)
{
if (tipo.equals("Mobile"))
{
MiReproductor = SingletonMP3.getMP3(cades, index, II);
}

if (tipo.equals("PC"))
{
// implementación para pc al rato la subo ;)
}

return MiReproductor;
}

}
Archivo: /home/oscar/NetBeansProjects/…heco/src/podcast/RepMP3_Clases Página 2 de 8

// ************************************* Interface Reproductor ****************************

package podcast;

public interface Reproductor {

public void play();

public void stop();

// ************ Clase SingletonMP3 que implementa a PlayerListener y Reproductor ********

package podcast;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Vector;
import javax.microedition.lcdui.*;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VolumeControl;

public class SingletonMP3 implements PlayerListener, Reproductor {

private static SingletonMP3 MP3;


private static String path = "/hello/";
private static Player player;
private static InputStream is;
private static String cades[][] = { };

private static int indice = 0;


private static ImageItem imageItem;
private static Image image;

private SingletonMP3(String cadesArray[][], int indxArray, ImageItem imageItm)


{
imageItem = imageItm;
cades = cadesArray;
indice = indxArray;

public static SingletonMP3 getMP3(String cadesArray[][], int indxArray, ImageItem imageItm)


{
if (MP3 == null)
{
MP3 = new SingletonMP3(cadesArray, indxArray, imageItm);
}
else
{
close();

imageItem = imageItm;
cades = cadesArray;
indice = indxArray;
}

return MP3;
Archivo: /home/oscar/NetBeansProjects/…heco/src/podcast/RepMP3_Clases Página 3 de 8

public void playEmbebdedFile()


{

if ( imageItem != null )
{
imageItem.setImage(getImage(SingletonMP3.indice));
}

try {
is = getClass().getResourceAsStream(cades[SingletonMP3.indice][0]);

player = Manager.createPlayer(is, "audio/mpeg");

player.realize();
VolumeControl vc = (VolumeControl) player.getControl("VolumeControl");

if (vc != null)
{
vc.setLevel(100);
}

player.prefetch();
player.addPlayerListener(MP3);
player.start();

}
catch (Exception e) { System.out.println("Error"); }
}

public void playerUpdate(Player player, String event, Object eventData)


{

if (event.equals(PlayerListener.END_OF_MEDIA) && indice < this.cades.length -1)


{
this.indice++;
SingletonMP3.getMP3(cades, SingletonMP3.indice, imageItem).playEmbebdedFile();

private static Image getImage( int in)


{
try
{
image = Image.createImage(path+cades[in][1]);
}
catch (java.io.IOException e)
{
e.printStackTrace();
}
return image;
}

public static void close()


{
if (player != null) {
player.close();
}
Archivo: /home/oscar/NetBeansProjects/…heco/src/podcast/RepMP3_Clases Página 4 de 8

public void play()


{
playEmbebdedFile();
}

public void stop()


{
close();
}

//********************************* GUI ***************************************************

package podcast;

import java.io.InputStream;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VolumeControl;

public class HelloMIDlet extends MIDlet implements CommandListener {

private boolean midletPaused = false;

String cades[][] = {
{"00.mp3", "00.jpg"},
{"01.mp3", "01.jpg"},
{"02.mp3", "02.jpg"},
{"03.mp3", "03.jpg"}

};
int indice = 0;
//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
private Command exitCommand;
private Command okCommand;
private Form form;
private ImageItem imageItem;
private Image image;
//</editor-fold>//GEN-END:|fields|0|

public HelloMIDlet() {
}

//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|


//</editor-fold>//GEN-END:|methods|0|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-


initialize|0|0-preInitialize
/**
* Initilizes the application.
* It is called only once when the MIDlet is started. The method is called before the
<code>startMIDlet</code> method.
*/
private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
// write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
Archivo: /home/oscar/NetBeansProjects/…heco/src/podcast/RepMP3_Clases Página 5 de 8

// write post-initialize user code here


}//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-


startMIDlet|0|3-preAction
/**
* Performs an action assigned to the Mobile Device - MIDlet Started point.
*/
public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
// write pre-action user code here
switchDisplayable(null, getForm());//GEN-LINE:|3-startMIDlet|1|3-postAction
// write post-action user code here
}//GEN-BEGIN:|3-startMIDlet|2|
//</editor-fold>//GEN-END:|3-startMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-


resumeMIDlet|0|4-preAction
/**
* Performs an action assigned to the Mobile Device - MIDlet Resumed point.
*/
public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
// write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
// write post-action user code here
}//GEN-BEGIN:|4-resumeMIDlet|2|
//</editor-fold>//GEN-END:|4-resumeMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-


switchDisplayable|0|5-preSwitch
/**
* Switches a current displayable in a display. The <code>display</code> instance is taken from
<code>getDisplay</code> method. This method is used by all actions in the design for switching
displayable.
* @param alert the Alert which is temporarily set to the display; if <code>null</code>, then
<code>nextDisplayable</code> is set immediately
* @param nextDisplayable the Displayable to be set
*/
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-
switchDisplayable|0|5-preSwitch
// write pre-switch user code here
Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}//GEN-END:|5-switchDisplayable|1|5-postSwitch
// write post-switch user code here
}//GEN-BEGIN:|5-switchDisplayable|2|
//</editor-fold>//GEN-END:|5-switchDisplayable|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//


GEN-BEGIN:|7-commandAction|0|7-preCommandAction
/**
* Called by a system to indicated that a command has been invoked on a particular displayable.
* @param command the Command that was invoked
* @param displayable the Displayable where the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-
preCommandAction
// write pre-action user code here
if (displayable == form) {//GEN-BEGIN:|7-commandAction|1|19-preAction
if (command == exitCommand) {//GEN-END:|7-commandAction|1|19-preAction
// write pre-action user code here
exitMIDlet();//GEN-LINE:|7-commandAction|2|19-postAction
// write post-action user code here
} else if (command == okCommand) {//GEN-LINE:|7-commandAction|3|23-preAction
Archivo: /home/oscar/NetBeansProjects/…heco/src/podcast/RepMP3_Clases Página 6 de 8

// write pre-action user code here

fabrica.getMiReproductor("Mobile", cades, indice, imageItem).play();

//GEN-LINE:|7-commandAction|4|23-postAction
// write post-action user code here
}//GEN-BEGIN:|7-commandAction|5|7-postCommandAction
}//GEN-END:|7-commandAction|5|7-postCommandAction
// write post-action user code here
}//GEN-BEGIN:|7-commandAction|6|
//</editor-fold>//GEN-END:|7-commandAction|6|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|


0|18-preInit
/**
* Returns an initiliazed instance of exitCommand component.
* @return the initialized component instance
*/
public Command getExitCommand() {
if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
// write pre-init user code here
exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
// write post-init user code here
}//GEN-BEGIN:|18-getter|2|
return exitCommand;
}
//</editor-fold>//GEN-END:|18-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">//GEN-BEGIN:|14-getter|0|14-


preInit
/**
* Returns an initiliazed instance of form component.
* @return the initialized component instance
*/
public Form getForm() {
if (form == null) {//GEN-END:|14-getter|0|14-preInit
// write pre-init user code here
form = new Form("MiPlayer", new Item[] { getImageItem() });//GEN-BEGIN:|14-getter|1|14-
postInit
form.addCommand(getExitCommand());
form.addCommand(getOkCommand());
form.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
// write post-init user code here
}//GEN-BEGIN:|14-getter|2|
return form;
}
//</editor-fold>//GEN-END:|14-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|22-getter|0|


22-preInit
/**
* Returns an initiliazed instance of okCommand component.
* @return the initialized component instance
*/
public Command getOkCommand() {
if (okCommand == null) {//GEN-END:|22-getter|0|22-preInit
// write pre-init user code here
okCommand = new Command("Play", Command.OK, 0);//GEN-LINE:|22-getter|1|22-postInit
// write post-init user code here
}//GEN-BEGIN:|22-getter|2|
return okCommand;
}
//</editor-fold>//GEN-END:|22-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: imageItem ">//GEN-BEGIN:|24-getter|0|


Archivo: /home/oscar/NetBeansProjects/…heco/src/podcast/RepMP3_Clases Página 7 de 8

24-preInit
/**
* Returns an initiliazed instance of imageItem component.
* @return the initialized component instance
*/
public ImageItem getImageItem() {
if (imageItem == null) {//GEN-END:|24-getter|0|24-preInit
// write pre-init user code here
imageItem = new ImageItem("", getImage(), ImageItem.LAYOUT_DEFAULT, "<Missing Image>");//GEN-
LINE:|24-getter|1|24-postInit
// write post-init user code here
}//GEN-BEGIN:|24-getter|2|
return imageItem;
}
//</editor-fold>//GEN-END:|24-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: image ">//GEN-BEGIN:|25-getter|0|25-


preInit
/**
* Returns an initiliazed instance of image component.
* @return the initialized component instance
*/
public Image getImage() {
if (image == null) {//GEN-END:|25-getter|0|25-preInit
// write pre-init user code here
try {//GEN-BEGIN:|25-getter|1|25-@java.io.IOException
image = Image.createImage("/hello/e_tux2.png");
} catch (java.io.IOException e) {//GEN-END:|25-getter|1|25-@java.io.IOException
e.printStackTrace();
}//GEN-LINE:|25-getter|2|25-postInit
// write post-init user code here
}//GEN-BEGIN:|25-getter|3|
return image;
}
//</editor-fold>//GEN-END:|25-getter|3|

/**
* Returns a display instance.
* @return the display instance.
*/
public Display getDisplay () {
return Display.getDisplay(this);
}

/**
* Exits MIDlet.
*/
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}

/**
* Called when MIDlet is started.
* Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
*/
public void startApp() {
if (midletPaused) {
resumeMIDlet ();
} else {
initialize ();
startMIDlet ();
}
midletPaused = false;
}
Archivo: /home/oscar/NetBeansProjects/…heco/src/podcast/RepMP3_Clases Página 8 de 8

/**
* Called when MIDlet is paused.
*/
public void pauseApp() {
midletPaused = true;
}

/**
* Called to signal the MIDlet to terminate.
* @param unconditional if true, then the MIDlet has to be unconditionally terminated and all
resources has to be released.
*/
public void destroyApp(boolean unconditional) {
}

You might also like