Java Tutorial/J2ME/Tones

Материал из Java эксперт
Версия от 05:00, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Distributed Tone

import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.midlet.MIDlet;
public class DistributedToneMIDlet extends MIDlet {
  private Player tonePlayer;
  
  public DistributedToneMIDlet() {
    try {
      tonePlayer = Manager.createPlayer(getClass().getResourceAsStream(
        "/x.jts"), "audio/x-tone-seq");
    } catch(Exception e) {
      System.err.println(e);
    }
  }
  
  public void startApp() {
    try {
      if(tonePlayer != null)
        tonePlayer.start();
    } catch(Exception e) {
      System.err.println(e);
    }
  }
  
  public void pauseApp() {
  }
  
  public void destroyApp(boolean unconditional) {
  }
}





Play CSharp

import javax.microedition.midlet.MIDlet;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
public class CSharpMIDlet extends MIDlet {
  public CSharpMIDlet() {    
  }
  
  public void startApp() {
    try {
      Manager.playTone(61, 5000, 100);
    } catch(MediaException me) { System.err.println(me); }
  }
  
  public void pauseApp() {
  }
  
  public void destroyApp(boolean unconditional) {
  }
  
}





Play tone

import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.media.Manager;
import javax.microedition.midlet.MIDlet;
public class J2MEAllTonesPlayer extends MIDlet implements CommandListener {
  Form displayForm = new Form("Playing all tones");
  StringItem info = new StringItem("", "");
  Command exit = new Command("Exit", Command.EXIT, 1);
  Thread runner;
  boolean stop = false;
  public void startApp() {
    displayForm.append(info);
    Display.getDisplay(this).setCurrent(displayForm);
    displayForm.addCommand(exit);
    displayForm.setCommandListener(this);
    runner = new Thread(new TonePlayer(info, this));
    runner.start();
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
    if (runner != null)
      stop = true;
  }
  public void commandAction(Command cmd, Displayable disp) {
    destroyApp(true);
    notifyDestroyed();
  }
}
class TonePlayer implements Runnable {
  StringItem info;
  AllTonesPlayer midlet;
  public TonePlayer(StringItem info, AllTonesPlayer midlet) {
    this.info = info;
    this.midlet = midlet;
  }
  public void run() {
    try {
      for (int i = 0; i < 128; i++) {
        Thread.sleep(1000);
        info.setText("Playing: " + i);
        Manager.playTone(i, 500, 100);
        if (midlet.stop)
          break;
      }
    } catch (Exception me) {
      System.err.println(me);
    }
  }
}