⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nokiasound.java

📁 《J2ME Game Programming》随书光盘源代码
💻 JAVA
字号:
import com.nokia.mid.sound.Sound;
import com.nokia.mid.sound.SoundListener;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;

/**
 * A demonstration of the Nokia UI Sound API.
 * @author Martin J. Wells
 */

public class NokiaSound extends MIDlet implements CommandListener, SoundListener
{
   private Sound sound;
   private Form form;
   private Command quit;
   private Command play;

   /**
    * MIDlet constructor creates a form and appends two commands. It then
    * instantiates a Nokia Sound class ready to play sounds for us in response
    * to the play command (see the commandAction method).
    */
   public NokiaSound()
   {
      form = new Form("Nokia Sound");

      form.setCommandListener(this);
      play = new Command("Play", Command.SCREEN, 1);
      form.addCommand(play);
      quit = new Command("Quit", Command.EXIT, 2);
      form.addCommand(quit);

      // Initialise a sound object we'll use later. We create the object here
      // with meaningless values becuase we'll init it later with proper
      // numbers. A sound listener is then set.
      sound = new Sound(0, 1L);
      sound.setSoundListener(this);
   }

   /**
    * The Nokia SoundListener interface method which is called when a sound
    * changes state.
    * @param sound The sound that changed state
    * @param state The state it changed to
    */
   public void soundStateChanged(Sound sound, int state)
   {
      if (state == Sound.SOUND_UNINITIALIZED)
         form.append("Sound uninitialized ");

      if (state == Sound.SOUND_PLAYING)
         form.append("Sound playing ");

      if (state == Sound.SOUND_STOPPED)
         form.append("Sound stopped ");
   }


   /**
    * Called by the Application Manager when the MIDlet is starting or resuming
    * after being paused. In this example it acquires the current Display object
    * and uses it to set the Form object created in the MIDlet constructor as
    * the active Screen to display.
    * @throws MIDletStateChangeException
    */
   protected void startApp() throws MIDletStateChangeException
   {
      Display.getDisplay(this).setCurrent(form);
   }

   /**
    * Called by the MID's Application Manager to pause the MIDlet. A good
    * example of this is when the user receives an incoming phone call whilst
    * playing your game. When they're done the Application Manager will call
    * startApp to resume. For this example we don't need to do anything.
    */
   protected void pauseApp()
   {
   }

   /**
    * Called by the MID's Application Manager when the MIDlet is about to
    * be destroyed (removed from memory). You should take this as an opportunity
    * to clear up any resources and save the game. For this example we don't
    * need to do anything.
    * @param unconditional if false you have the option of throwing a
    * MIDletStateChangeException to abort the destruction process.
    * @throws javax.microedition.midlet.MIDletStateChangeException
    */
   protected void destroyApp(boolean unconditional)
           throws MIDletStateChangeException
   {
   }

   /**
    * The CommandListener interface method called when the user executes a
    * Command, in this case it can only be the quit command we created in the
    * consutructor and added to the Form.
    * @param command the command that was triggered
    * @param displayable the displayable on which the event occurred
    */
   public void commandAction(Command command, Displayable displayable)
   {
      // check for our quit command and act accordingly
      try
      {
         if (command == play)
         {
            // initliase the parameters of the sound (440Hz playing for 2
            // seconds - 2000 ms)
            sound.init(440, 2000L);
            sound.play(1);
         }

         if (command == quit)
         {
            destroyApp(true);

            // tell the Application Manager we're exiting
            notifyDestroyed();
         }
      }

      // we catch this even though there's no chance it will be thrown
      // since we called destroyApp with unconditional set to true.
      catch (MIDletStateChangeException me)
      { }
   }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -