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

📄 audio.java

📁 --- --- --- 基于J2ME的游戏程序--------很有技巧性的程序
💻 JAVA
字号:
// Copyright (c) 2005 Sony Ericsson Mobile Communications AB
//
// This software is provided "AS IS," without a warranty of any kind. 
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, 
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A 
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. 
//
// THIS SOFTWARE IS COMPLEMENTARY OF JAYWAY AB (www.jayway.se)

package bluegammon;

import java.io.IOException;

import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;


/**
 * <p>
 * Audio manager. Contains one player for each sound.
 * </p><p>
 * Implementation note: the format of the sound is hardcoded
 * in method <code>playSound</code>.
 * </p>
 * 
 * @author Peter Andersson
 */
public class Audio
{
  /** Jazzy background music */
  public static final int MUSIC = 0;
  /** Dices hitting board, long sound */
  public static final int DICES_LONG = 1;
  /** Dices hitting board, short sound */
  public static final int DICES_SHORT = 2;
  /** Piece sound */
  public static final int PIECE = 3;
  /** Message sound */
  public static final int MESSAGE = 4;
  /** Connection failed sound */
  public static final int CONN_FAIL = 5;
  /** Winner sound */
  public static final int WINNER = 6;
  /** Loser sound */
  public static final int LOSER = 7;
  
  /** The media players */
  protected static Player[] m_sounds = new Player[8];
  /** Current playing player index */
  protected static int m_currentPlayer = -1;
  
  protected static final String TYPE_AMR = "audio/amr";
  protected static final String TYPE_MIDI = "audio/midi";
  
  /**
   * Starts playing specified sound, one of <code>Audio.MUSIC</code>, 
   * <code>Audio.DICES_LONG</code>, <code>Audio.DICES_SHORT</code>,
   * <code>Audio.PIECE</code>. If there is another sound playing,
   * it is stopped.
   * @param snd		The id of the sound to play.
   */
  public synchronized static void playSound(int snd)
  {
    // Start sound
    if (!RmsFacade.getBoolean(Bluegammon.AUDIO_OFF))
    {
      // No player, create one
      if (m_sounds[snd] == null)
      {
        createSound(snd);
      }
      
      // Start player
      Player player = m_sounds[snd];
      if (player != null)
      {
        try
        {
          player.start();
          m_currentPlayer = snd;
        }
        catch (MediaException e)
        {
          e.printStackTrace();
        }
      }
    }
  }
  
  /**
   * Stops specified sound if it is playing.
   * @param snd		The id of the sound to stop.
   */
  public synchronized static void stopSound(int snd)
  {
    if (m_sounds[snd] != null)
    {
      try
      {
        m_sounds[snd].stop();
      }
      catch (MediaException e)
      {
        e.printStackTrace();
      }
    }
  }

  /**
   * Stops all sounds and cleans up resources.
   */
  public static void shutdown()
  {
    for (int i = 0; i < m_sounds.length; i++)
    {
      stopSound(i);
      if (m_sounds[i] != null)
      {
        m_sounds[i].deallocate();
      }
    }
  }
  
  /**
   * Creates a player for specified sound
   * and popuplates the Player array.
   * @param snd	The sound to create
   */
  protected static void createSound(int snd)
  {
    try
    {
      int rsc = 0;
      String type = TYPE_AMR;
      switch(snd)
      {
      case MUSIC:
        type = TYPE_MIDI;
        rsc = Resources.SND_JAZZY;
        break;
      case MESSAGE:
        type = TYPE_MIDI;
        rsc = Resources.SND_MESSAGE;
        break;
      case CONN_FAIL:
        type = TYPE_MIDI;
        rsc = Resources.SND_CONNFAIL;
        break;
      case WINNER:
        type = TYPE_MIDI;
        rsc = Resources.SND_WINNER;
        break;
      case LOSER:
        type = TYPE_MIDI;
        rsc = Resources.SND_LOSER;
        break;
      case DICES_LONG:
        rsc = Resources.SND_DICES_LONG;
        break;
      case DICES_SHORT:
        rsc = Resources.SND_DICES_SHORT;
        break;
      case PIECE:
        rsc = Resources.SND_PIECE;
        break;
      }
      m_sounds[snd] = 
        Manager.createPlayer(Resources.getResource(rsc), type);
      if (snd == MUSIC)
      {
        m_sounds[snd].setLoopCount(-1);		// loop intro tune for ever and ever
      }
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    catch (MediaException e)
    {
      e.printStackTrace();
    }
  }

  /** Prevent instantiation */
  private Audio() {}
}

⌨️ 快捷键说明

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