Вы находитесь на странице: 1из 14

Video Recoding import import import import import java.io.*; javax.microedition.midlet.*; javax.microedition.lcdui.*; javax.microedition.media.*; javax.microedition.media.control.

*;

public class VoiceRecordMidlet extends MIDlet { private Display display; public void startApp() { display = Display.getDisplay(this); display.setCurrent(new VoiceRecordForm()); } public void pauseApp() { } public void destroyApp(boolean unconditional) { notifyDestroyed(); } } class VoiceRecordForm extends Form implements CommandListener { private StringItem message; private StringItem errormessage; private final Command record, play; private Player player; private byte[] recordedAudioArray = null; public VoiceRecordForm() { super("Recording Audio"); message = new StringItem("", "Select Record to start recording."); this.append(message); errormessage = new StringItem("", ""); this.append(errormessage); record = new Command("Record", Command.OK, 0); this.addCommand(record); play = new Command("Play", Command.BACK, 0); this.addCommand(play); this.setCommandListener(this); } public void commandAction(Command comm, Displayable disp) { if (comm == record) { Thread t = new Thread() { public void run() { try { player = Manager.createPlayer("capture://aud io?encoding=pcm"); player.realize(); RecordControl rc = (RecordControl) player.ge tControl("RecordControl"); ByteArrayOutputStream output = new ByteArray OutputStream(); rc.setRecordStream(output); rc.startRecord(); player.start(); message.setText("Recording..."); Thread.sleep(5000); message.setText("Recording Done!");

rc.commit(); recordedAudioArray = output.toByteArray(); player.close(); } catch (Exception e) { errormessage.setLabel("Error"); errormessage.setText(e.toString()); } } }; t.start(); } else if (comm == play) { try { ByteArrayInputStream recordedInputStream = new ByteArray InputStream(recordedAudioArray); Player p2 = Manager.createPlayer(recordedInputStream, "a udio/basic"); p2.prefetch(); p2.start(); } catch (Exception e) { errormessage.setLabel("Error"); errormessage.setText(e.toString()); } } } } Frame Animation import import import import java.util.Timer; java.util.TimerTask; javax.microedition.lcdui.*; javax.microedition.midlet.*;

public class FrameAnimation extends MIDlet implements CommandListener, ItemStateListener{ private Display display; protected boolean started; private Command exit, setup, run; private Form form; private AnimationCanvas canvas; private Gauge block, rate; private static final int FRAME_RATE = 1; private static final int BLOCK_COUNT = 1; protected void startApp() { if (!started) { display = Display.getDisplay(this); form = new Form("Frame Animation"); rate = new Gauge("Set Frame", true, 10, FRAME_RATE); block = new Gauge("Set Blocks", true, 4, BLOCK_COUNT); form.append(rate); form.append(block); form.setItemStateListener(this); canvas = createAnimationCanvas(); exit = new Command("Exit", Command.EXIT, 0); setup = new Command("Setup", Command.SCREEN, 0); run = new Command("Run", Command.SCREEN, 0);

canvas.addCommand(exit); canvas.addCommand(setup); form.addCommand(exit); form.addCommand(run); form.setCommandListener(this); canvas.setCommandListener(this); display.setCurrent(form); started = true; } } protected void pauseApp(){} protected void destroyApp(boolean unconditional){} public void commandAction(Command c, Displayable d){ String label = c.getLabel(); if (label.equals("Exit")){ notifyDestroyed(); } else if (label.equals("Run")){ display.setCurrent(canvas); } else if (label.equals("Setup")){ display.setCurrent(form); } } public void itemStateChanged(Item item){ if (item == block){ int count = block.getValue(); if(count < 1) { count = 1; } canvas.setBlockCount(count); } else if (item == rate){ int count = rate.getValue(); if (count < 1){ count = 1; } canvas.setFrameRate(count); } } protected AnimationCanvas createAnimationCanvas(){ return new AnimationCanvas(); } class AnimationCanvas extends Canvas{ protected static final int SIZE = 4; protected final int[] xSpeeds = { 2, -2, 0, -2 protected final int[] ySpeeds = { 2, -2, 2, -0 protected int background = display.isColor() ? protected int foreground = display.isColor() ? protected int width = getWidth(); protected int height = getHeight(); protected int frameRate; protected Block[] blocks; protected Timer timer;

}; }; 0x6699ff : 0xc0c0c0; 0xff3300 : 0;

protected TimerTask updateTask; public int getMaxBlocks(){ return blocks.length; } AnimationCanvas(){ setBlockCount(BLOCK_COUNT); setFrameRate(FRAME_RATE); } public void setBlockCount(int count){ blocks = new Block[count]; createBlocks(); } public int getBlockCount(){ return blocks.length; } public void setFrameRate(int frameRate){ this.frameRate = frameRate; if (isShown()){ startFrameTimer(); } } public int getFrameRate(){ return frameRate; } protected void paint(Graphics g){ g.setColor(background); g.fillRect(0, 0, width, height); g.setColor(foreground); synchronized (this){ for (int i = 0, count = blocks.length; i < count; i++){ g.fillRect(blocks[i].x, blocks[i].y, SIZE, SIZE); } } } protected void showNotify(){ startFrameTimer(); } protected void hideNotify(){ stopFrameTimer(); } private void createBlocks(){ int startX = (width - SIZE)/2; int startY = (height - SIZE)/2; for (int i = 0, count = blocks.length; i < count; i++){ blocks[i] = new Block(startX, startY, xSpeeds[i], ySpeeds[i]); } } protected void startFrameTimer(){ timer = new Timer(); updateTask = new TimerTask(){ public void run() { moveAllBlocks(); } };

long interval = 1000/frameRate; timer.schedule(updateTask, interval, interval); } protected void stopFrameTimer(){ timer.cancel(); } public synchronized void moveAllBlocks(){ for (int i = 0, count = blocks.length; i < count; i++){ blocks[i].move(); repaint(); } } class Block{ int x; int y; int xSpeed; int ySpeed; Block(int x, int y, int xSpeed, int ySpeed){ this.x = x; this.y = y; this.xSpeed = xSpeed; this.ySpeed = ySpeed; } void move(){ x += xSpeed; if(x <= 0 || x + SIZE >= width){ xSpeed = -xSpeed; } y += ySpeed; if(y <= 0 || y + SIZE >= height){ ySpeed = -ySpeed; } } } } } Timer import java.util.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class TimerMIDlet extends MIDlet implements CommandListener{ private Display display; private Form form; private Command exit, stop; private Timer timer; private TestTimerTask task; private int count = 0; public TimerMIDlet(){ display = Display.getDisplay(this);

form = new Form("Timer Example"); exit = new Command("Exit", Command.EXIT, 1); stop= new Command("Stop", Command.STOP, 2); form.append("Please wait for timer.. \n"); form.addCommand(exit); form.addCommand(stop); form.setCommandListener(this); } public void startApp (){ timer = new Timer(); task = new TestTimerTask(); timer.schedule(task,5000); display.setCurrent(form); } public void pauseApp (){ } public void destroyApp (boolean unconditional){ notifyDestroyed(); } public void commandAction(Command c, Displayable d){ String label = c.getLabel(); if (label.equals("Stop")){ timer.cancel(); }else if (label.equals("Exit")) { destroyApp(true); } } private class TestTimerTask extends TimerTask{ public final void run(){ form.append("Timer Execute Count: " + ++count + "\n"); } } } Video Player import import import import import import java.io.*; java.util.*; javax.microedition.lcdui.*; javax.microedition.midlet.*; javax.microedition.media.*; javax.microedition.media.control.*;

public class VideoPlayer extends MIDlet implements CommandListener, PlayerListener { private Display display; private List itemList; private Form form; private Command stop, pause, start; private Hashtable items, itemsInfo; private Player player; public VideoPlayer() { display = Display.getDisplay(this); itemList = new List("Select an item to play", List.IMPLICIT); stop = new Command("Stop", Command.STOP, 1);

pause = new Command("Pause", Command.ITEM, 1); start = new Command("Start", Command.ITEM, 1); form = new Form("Playing video"); form.addCommand(stop); form.addCommand(pause); form.setCommandListener(this); items = new Hashtable(); itemsInfo = new Hashtable(); items.put("SpringWaterFall...", "file://SpringWaterFall.mpg"); itemsInfo.put("SpringWaterFall...", "video/mpeg"); items.put("helloboy...", "file://helloboy.mpg"); itemsInfo.put("helloboy...", "video/mpeg"); items.put("pilgrim...", "file://pilgrim.mpg"); itemsInfo.put("pilgrim...", "video/mpeg"); items.put("pirates...", "file://pirates.mpg"); itemsInfo.put("pirates...", "video/mpeg"); items.put("pythag1...", "file://pythag1.mpg"); itemsInfo.put("pythag1...", "video/mpeg"); items.put("CarelessEnglish...", "file://CarelessEnglish.mpg"); itemsInfo.put("CarelessEnglish...", "video/mpeg"); } public void startApp() { for(Enumeration en = items.keys(); en.hasMoreElements();) { itemList.append((String)en.nextElement(), null); } itemList.setCommandListener(this); display.setCurrent(itemList); } public void pauseApp() { try { if(player != null) player.stop(); } catch(Exception e) {} } public void destroyApp(boolean unconditional) { if(player != null) player.close(); } public void commandAction(Command c, Displayable d){ if(d instanceof List) { List list = ((List)d); String key = list.getString(list.getSelectedIndex()); try { playAudio((String)items.get(key), key); } catch (Exception e) { System.err.println("Unable to play: " + e); e.printStackTrace(); } } else if(d instanceof Form){ try { if(c == stop){ player.close();

display.setCurrent(itemList); form.removeCommand(start); form.addCommand(pause); } else if(c == pause){ player.stop(); form.removeCommand(pause); form.addCommand(start); } else if(c == start){ player.start(); form.removeCommand(start); form.addCommand(pause); } } catch(Exception e) { System.err.println(e); } } } private void playAudio(String locator, String key) throws Exception { String file = locator.substring(locator.indexOf("file://") + 6, locator.length()); player = Manager.createPlayer(getClass().getResourceAsStream(file), (String)itemsInfo.get(key)); player.addPlayerListener(this); player.setLoopCount(-1); player.prefetch(); player.realize(); player.start(); } public void playerUpdate(Player player, String event, Object eventData) { if(event.equals(PlayerListener.STARTED) && new Long(0L).equals((Long) eventData)) { VideoControl vc = null; if((vc = (VideoControl)player.getControl("VideoControl")) != null) { Item videoDisp = (Item)vc.initDisplayMode(vc.USE_GUI_PRIMITIVE, null); form.append(videoDisp); } display.setCurrent(form); } else if(event.equals(PlayerListener.CLOSED)) { form.deleteAll(); } } } Media Midlet import import import import import java.io.*; java.util.*; javax.microedition.lcdui.*; javax.microedition.midlet.*; javax.microedition.media.*;

public class MediaMIDlet extends MIDlet implements CommandListener, PlayerListener{ private Display display; private List itemList; private Form form; private Command stopCommand, pauseCommand, startCommand; private Hashtable items, itemsInfo;

private Player player; public MediaMIDlet() { display = Display.getDisplay(this); itemList = new List("Select an item to play", List.IMPLICIT); stopCommand = new Command("Stop", Command.STOP, 1); pauseCommand = new Command("Pause", Command.ITEM, 1); startCommand = new Command("Start", Command.ITEM, 1); form = new Form("Playing media"); form.addCommand(stopCommand); form.addCommand(pauseCommand); form.setCommandListener(this); items = new Hashtable(); itemsInfo = new Hashtable(); items.put("Kyo Aage-Piche Dolte Ho...", "file://aagepiche.wav"); itemsInfo.put("Kyo Aage-Piche Dolte Ho...", "audio/x-wav"); items.put("kabhi alvida-na-kehna...", "file://kabhi-alvida-na-kehna.wav"); itemsInfo.put("kabhi alvida-na-kehna...", "audio/x-wav"); items.put("Lucky Boy...", "file://Lucky-Boy.wav"); itemsInfo.put("Lucky Boy...", "audio/x-wav"); items.put("Move Your Body...", "file://Move-Your-Body.wav"); itemsInfo.put("Move Your Body...", "audio/x-wav"); items.put("Jee Karda...", "file://Jee-Karda.wav"); itemsInfo.put("Jee Karda...", "audio/x-wav"); items.put("masoomchehra...", "file://masoomchehra.wav"); itemsInfo.put("masoomchehra...", "audio/x-wav"); items.put("tu-saala...", "file://tu-saala.wav"); itemsInfo.put("tu-saala...", "audio/x-wav"); } public void startApp() { for(Enumeration en = items.keys(); en.hasMoreElements();) { itemList.append((String)en.nextElement(), null); } itemList.setCommandListener(this); display.setCurrent(itemList); } public void pauseApp() { try { if(player != null) player.stop(); } catch(Exception e) {} } public void destroyApp(boolean unconditional) { if(player != null) player.close(); } public void commandAction(Command command, Displayable disp){ if(disp instanceof List) { List list = ((List)disp); String key = list.getString(list.getSelectedIndex()); try {

playMedia((String)items.get(key), key); } catch (Exception e) { System.err.println("Unable to play: " + e); e.printStackTrace(); } } else if(disp instanceof Form){ try { if(command == stopCommand){ player.close(); display.setCurrent(itemList); form.removeCommand(startCommand); form.addCommand(pauseCommand); } else if(command == pauseCommand){ player.stop(); form.removeCommand(pauseCommand); form.addCommand(startCommand); } else if(command == startCommand){ player.start(); form.removeCommand(startCommand); form.addCommand(pauseCommand); } } catch(Exception e) { System.err.println(e); } } } private void playMedia(String locator, String key) throws Exception { String file = locator.substring(locator.indexOf("file://") + 6, locator.length()); player = Manager.createPlayer(getClass().getResourceAsStream(file), (String)itemsInfo.get(key)); player.addPlayerListener(this); player.setLoopCount(-1); player.prefetch(); player.realize(); player.start(); } public void playerUpdate(Player player, String event, Object eventData) { if(event.equals(PlayerListener.STARTED) && new Long(0L).equals((Long) eventData)){ display.setCurrent(form); } else if(event.equals(PlayerListener.CLOSED)) { form.deleteAll(); } } } Audio Midlet import java.io.*; import javax.microedition.lcdui.*; import javax.microedition.media.*; import javax.microedition.midlet.*; //import javax.microedition.media.TimeBase; public class AudioMIDlet extends MIDlet{ Player p1, p2;

public void startApp(){ try { p1 = Manager.createPlayer(getClass().getResourceAsStream("/kabhi-alvida-na-keh na.wav"), "audio/x-wav"); p1.realize(); p2 = Manager.createPlayer(getClass().getResourceAsStream("/aagepiche.wav"), "a udio/x-wav"); p2.realize(); //p2.setTimeBase(p1.getTimeBase()); p1.prefetch(); p2.prefetch(); p1.start(); p2.start(); }catch(IOException ioe){ }catch(MediaException me){} } public void pauseApp(){} public void destroyApp(boolean unconditional){} } Access URL import import import import java.io.*; javax.microedition.io.*; javax.microedition.lcdui.*; javax.microedition.midlet.*;

public class AccessUrl extends MIDlet{ private Display display; String url = "http://www.roseindia.net/hello.txt"; public AccessUrl(){ display = Display.getDisplay(this); } public void startApp(){ try{ connection(url); } catch (IOException e){ System.out.println("IOException " + e); e.printStackTrace(); } } public void pauseApp(){} public void destroyApp(boolean unconditional){} void connection(String url) throws IOException{ StreamConnection sc = null; InputStream is = null;

StringBuffer buffer = new StringBuffer(); TextBox access; try{ sc = (StreamConnection)Connector.open(url); is = sc.openInputStream(); int chars; while((chars = is.read()) != -1){ buffer.append((char) chars); } System.out.println(buffer.toString()); access = new TextBox("Access Text", buffer.toString(), 1024, 0); }finally{ if(is != null){ is.close(); } if(sc != null){ sc.close(); } } display.setCurrent(access); } } Text Example import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class BoxTextCanvas extends MIDlet{ private Display display; public void startApp(){ Canvas canvas = new CanvasBoxText(); display = Display.getDisplay(this); display.setCurrent(canvas); } public void pauseApp(){} public void destroyApp(boolean unconditional){} } class CanvasBoxText extends Canvas{ private Font font; public CanvasBoxText(){ font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_LARGE); } public void paint(Graphics g){ int width = getWidth(); int height = getHeight(); g.setColor(255, 0, 0); g.fillRect(0, 0, width, height); g.setColor(0, 0, 255); String sandeep = "SANDEEP"; int w = font.stringWidth(sandeep);

int h = font.getHeight(); int x = (width - w) / 2; int y = height / 2; g.setFont(font); g.drawString(sandeep, x, y, Graphics.TOP | Graphics.LEFT); g.drawRect(x, y, w, h); } } Record Store import import import import java.io.*; javax.microedition.rms.*; javax.microedition.midlet.*; javax.microedition.lcdui.*;

public class ReadWriteMIDlet extends MIDlet implements CommandListener{ private Display display; private Alert alert; private Form form; private Command exit, start; private RecordStore recordStore; public ReadWriteMIDlet(){ display = Display.getDisplay(this); start = new Command("Start", Command.SCREEN, 1); exit = new Command("Exit", Command.SCREEN, 1); form = new Form("Record"); form.addCommand(start); form.addCommand(exit); form.setCommandListener(this); } public void startApp(){ display.setCurrent(form); } public void pauseApp(){} public void destroyApp(boolean unconditional){ notifyDestroyed(); } public void commandAction(Command command, Displayable displayable){ if(command == exit){ destroyApp(true); }else if(command == start){ try{ recordStore = RecordStore.openRecordStore("Sandeep Kumar Suman", true ); String outputData = "First Record Completed..."; byte[] byteOutputData = outputData.getBytes(); recordStore.addRecord(byteOutputData, 0, byteOutputData.length); byte[] byteInputData = new byte[1]; int length = 0;

for (int x = 1; x <= recordStore.getNumRecords(); x++){ if (recordStore.getRecordSize(x) > byteInputData.length){ byteInputData = new byte[recordStore.getRecordSize(x)]; } length = recordStore.getRecord(x, byteInputData, 0); } alert = new Alert("Reading", new String(byteInputData, 0, length), null, AlertType.WARNING); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); recordStore.closeRecordStore(); }catch (Exception e){} if(RecordStore.listRecordStores() != null){ try{ RecordStore.deleteRecordStore("Sandeep Kumar Suman"); }catch (Exception e){} } } } }

Вам также может понравиться