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

📄 sound.java

📁 用java实现的类似于连连看游戏的单词连连看
💻 JAVA
字号:
import javax.sound.sampled.*;
import java.net.*;
public class Sound implements Runnable 
{

  String currentName;
  Object currentSound;
  Thread thread;
  String[] filename = {
      "Sound/clickerx.wav", "Sound/fail.wav", "Sound/itemboom.wav",
      "Sound/end.wav","Sound/start.wav"};

  public static int CLICKERX = 0;
  public static int FAIL = 1;
  public static int ITEMBOOM = 2;
  public static int END = 3;
  public static int START = 4;
  //public static int HINT = 4;

  public Sound(int sound) 
  {
    if (sound < 0 || sound > 4) 
    {
      return;
    }

    URLClassLoader urlLoader = (URLClassLoader)this.getClass().getClassLoader();
    URL url = urlLoader.findResource(filename[sound]);

    try 
    {
      currentSound = AudioSystem.getAudioInputStream(url);
    }
    catch (Exception ex1) 
    {
      currentSound = null;
      return;
    }

    if (currentSound instanceof AudioInputStream) 
    {
      try 
      {
        AudioInputStream stream = (AudioInputStream) currentSound;
        AudioFormat format = stream.getFormat();

        DataLine.Info info = new DataLine.Info(
            Clip.class,
            stream.getFormat(),
            ( (int) stream.getFrameLength() *
             format.getFrameSize()));

        Clip clip = (Clip) AudioSystem.getLine(info);
        clip.open(stream);
        currentSound = clip;
      }
      catch (Exception ex) 
      {
        currentSound = null;
        return;
      }
    }

    if (currentSound != null) 
    {
      start();
    }
  }

  public void playSound() 
  {
    if (currentSound instanceof Clip) {
      Clip clip = (Clip) currentSound;
      clip.start();
      try {
        thread.sleep(999);
      }
      catch (Exception e) {
      }
      while (clip.isActive() && thread != null) {
        try {
          thread.sleep(99);
        }
        catch (Exception e) {
          break;
        }
      }

      clip.stop();
      clip.close();
    }
    currentSound = null;
  }

  public void start() {
    thread = new Thread(this);
    thread.start();
  }

  public void run() 
  {
    playSound();
    stop();
  }

  public void stop() 
  {
    if (thread != null) 
    {
      thread.interrupt();
    }
    thread = null;
  }
}

⌨️ 快捷键说明

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