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

📄 soundplayer.java

📁 这个是早期学习时写的RPG游戏 包括地图相关 战斗相关 NPC与 存储等 功能
💻 JAVA
字号:

package perGame;
import java.io.*;
import javax.microedition.media.*;


public class SoundPlayer
{
  private static javax.microedition.media.Player curSound;

  static final int MAXSOUND = 0xff;
  static int soundGain = MAXSOUND;

  private javax.microedition.media.Player sound;

  public SoundPlayer(int index)
  {
    try
    {
      String a = "";
      DataInputStream dis;
      //得到声音文件
      dis = new DataInputStream(a.getClass().getResourceAsStream("/a" + index));

      if(index < 100)
      {
        sound = Manager.createPlayer(dis, "audio/midi");//声音对象
      }
      else
      {
        sound = Manager.createPlayer(dis, "audio/x-wav");
      }
    }
    catch(Exception e)
    {
      sound = null;
    }
  }

  public static void Stop()
  {
    /**@todo Implement this sdk.util.Sound abstract method*/
    if(curSound == null)
    {
      return;
    }

    try
    {
      curSound.stop();
    }
    catch(Exception e)
    {
    }

    try
    {
      curSound.deallocate();
    }
    catch(Exception e)
    {
      curSound = null;
    }

  }

  private boolean doForPlay()
  {
    if(sound == null)
    {
      return false;
    }
    if(soundGain <= 0)
    {
      return false;
    }

    Stop();
    return true;
  }

  public void loop()
  {
    if(this.soundGain == 0)
    {
      return;
    }
    /**@todo Implement this sdk.util.Sound abstract method*/
    if(!doForPlay())
    {
      return;
    }
    try
    {
      sound.setLoopCount(10000);
      sound.realize();
      sound.prefetch();
      sound.start();
      curSound = sound;
    }
    catch(Exception e)
    {
    }
  }

  public void play()
  {
    if(this.soundGain == 0)
    {
      return;
    }
    if(curSound != null)
    {
      if(curSound.getState() == javax.microedition.media.Player.STARTED /*Sound.SOUND_PLAYING*/)
      {
        return;
      }
    }
    playNew();
  }

  public void playNew()
  {
    if(this.soundGain == 0)
    {
      return;
    }
    if(!doForPlay())
    {
      return;
    }
    try
    {
      sound.setLoopCount(1);//设置播放的次数
      sound.realize();
      sound.prefetch();
      sound.start();//开始播放声音
      curSound = sound;
      //sound.play(1);
    }
    catch(Exception e)
    {
    }

  }

}

⌨️ 快捷键说明

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