//12.2.1ʹõTone
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

public class PlayToneMIDlet extends MIDlet implements CommandListener {

private Display display;
private List list;
private Command exitCommand =
                         new Command( "Exit", Command.EXIT, 1 );
private Command  playCommand =
                     new Command( "Play", Command.OK, 1 );

public PlayToneMIDlet(){
    display = Display.getDisplay(this);
    List = new List(Demo,List.IMPLICIT);
}

protected void startApp(){
list.addCommand(exitCommand);
list.addCommand(playCommand);
list.setCommandListener(this);
display.setCurrent(list);
}

protected void destroyApp( boolean unconditional ){
}

protected void pauseApp(){
}

protected void commandAction( Command c,Displayable d ){
     if(c== exitCommand){
        destroyApp(false);
        notifyDestroyed();
      }else
         play();
}

private void play(){
    try{
Manager.playTone(ToneControl.C4,120,60);
     }catch(MediaException e){
        }
     }
    }


//12.2.2
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
public class TonePlayer extends MIDlet implements CommandListener{

Player player;
Display display;
Form form;
Command exitCmd, closeCmd, stopCmd, startCmd;
ToneControl tControl;
byte tempo = (byte)15;
byte resolution = (byte)32;
byte C4 = (byte)ToneControl.C4;
byte D4 = (byte)(C4 + 2);
byte E4 = (byte)(C4 + 4);
byte F4 = (byte)(C4 + 5);
byte G4 = (byte)(C4 + 7);
byte[] toneData = {
ToneControl.VERSION, 1, ToneControl.TEMPO, tempo, 
ToneControl.RESOLUTION, resolution, 
G4, 4, E4, 4, E4, 8, F4, 4, D4, 4, D4, 8, 
C4, 4, D4, 4, E4, 4, F4, 4, G4, 4, G4, 4, G4, 8,
G4, 4, E4, 4, E4, 8, F4, 4, D4, 4, D4, 8, 
C4, 4, E4, 4, G4, 4, G4, 4, E4, 16,
D4, 4, D4, 4, D4, 4, D4, 4, D4, 4, E4, 4, F4, 8,
E4, 4, E4, 4, E4, 4, E4, 4, E4, 4, F4, 4, G4, 8, 
G4, 4, E4, 4, E4, 8, F4, 4, D4, 4, D4, 8,
C4, 4, E4, 4, G4, 4, G4, 4, C4, 16
}

public TonePlayer(){
display = Display.getDisplay(this);
form = new Form("MediaQuery");
try{
player = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);
}catch(Exception ex){}

exitCmd = new Command("Exit", Command.EXIT, 1);
closeCmd = new Command("Close", Command.SCREEN, 1);
startCmd = new Command("Start", Command.SCREEN, 2);
stopCmd = new Command("Stop", Command.SCREEN, 3);
form.addCommand(exitCmd);
form.addCommand(stopCmd);
form.addCommand(startCmd);
form.addCommand(closeCmd);
form.setCommandListener(this);
} 

public void startApp(){
display.setCurrent(form);
try{
player.realize();
tControl = (ToneControl)player.getControl("ToneControl");
if(tControl != null){
tControl.setSequence(toneData);
player.start();
}
}catch(Exception ex){}
}

public void pauseApp(){
}

public void destroyApp(boolean unconditional){
}

public void commandAction(Command c, Displayable d){
if(c == exitCmd){
destroyApp(true);
notifyDestroyed();
}else if(c == stopCmd){
try{
player.stop();
}catch(Exception ex){}
}else if(c == startCmd){
try{
player.start();
}catch(Exception ex){}
}else if(c == closeCmd){
try{
player.close();
}catch(Exception e){}
}
}
}


//12.2.3ʹƵԴ
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import java.io.*;

public class PlayMusic extends MIDlet {
private Player player = null;
public PlayMusic() {
try{
InputStream in =this.getClass().getResourceAsStream(/music.wav);
Player player = Manager.creatPlayer(in, audio/x-wav);
   }catch(IOException e){
   }catch(MediaException e){
   }catch(Exception e){
   }
}
public void startApp() {
if(player != null){
        try{
player.start();
}catch(MediaException e){
}
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional){
}
}


//12.2.4ƵԴ
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.VideoControl;
import java.io.*;

public class PlayVideo extends MIDlet implements CommandListener{
private Display display;
Form form = new Form(Ƶ);
    Command cmdPlay = new Command(play,Command.SCREEN,1);
    Command cmdExit = new Command(exit,Command.EXIT,1);
    Player player = null;

public PlayVideo(){
form.addCommand(cmdPlay);
form.addCommand(cmdExit);
form.setCommandListener(this);
}

public void startApp(){
display = Display.getDisplay(this);
display.setCurrent(form);
}
public void pauseApp(){
}

public void destroyApp(boolean unconditional)
throws MIDletStateChangeException{
close();
}

private void close(){
    if(player != null){
            player.close();
            player = null;
}
}

public void commandAction(Command c, Displayable d){
    if(c == cmdExit){
        notifyDestroyed();
}else if(c == cmdPlay){
       playCanvas();
}
}

private void playCanvas(){
    try{
         InputStream is = getClass().getResourceAsStream("/somefile.avi");
         player  =  Manager.createPlayer(is,"video/avi");
         player.realize();
         MyCanvas cav = new MyCanvas();
         VideoControl vc = (VideoControl)player.getControl("VideoControl");
vc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO,cav);
         display.setCurrent(cav);
         player.start();
}catch(Exception e){
    e.printStackTrace();
}
}

class MyCanvas extends Canvas{
protected void paint(Graphics p){
}
}
}
