应用程序中的 Java 游戏声音(如,非小程序)

问题描述 投票:0回答:4

我有两个问题 - (1) 如何播放小声音片段,例如飞碟飞行、子弹射击、物体被子弹击中等。非常短但实时的声音。我喜欢旧的街机声音,所以它们不需要很大的 .wav。我想用尽可能少的代码运行它们。这就引出了我的第二个问题…… (2) 有谁知道在哪里可以找到这些声音片段。

小提示,我在这里看到了一些答案,但它们似乎不完整。如果您有直接的通用代码,那就太好了!我对声音知之甚少,因为我通常不会在我的游戏写作中走这么远。

感谢您提供的所有信息 - 我非常感谢!

java audio javasound
4个回答
1
投票

使用javax.sound,可以实现简单的音效


编辑:使用线程(或不使用)

import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class AppWithSound2 extends JFrame implements ActionListener {
  JButton b1;
  JButton b2;

  private static final long serialVersionUID = 1L;

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        AppWithSound2 app = new AppWithSound2();
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        app.startApp();
      }
    });
  }

  public AppWithSound2() {
    initGUI();
  }

  private void startApp() {
    setVisible(true);
  }

  private void initGUI() {
    setLayout(new FlowLayout());
    setSize(300, 200);
    b1 = new JButton("Sound with no thread");
    b2 = new JButton("Sound with thread");
    b1.addActionListener(this);
    b2.addActionListener(this);
    add(b1);
    add(b2);
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == b1) {
      LaserSound.laser();
    }
    if (e.getSource() == b2) {
      new LaserSound().start();
    }
  }
}

class LaserSound extends Thread {

  public void run() {
    LaserSound.laser();
  }

  public static void laser() {
    int repeat = 10;
    try {
      AudioFormat af = new AudioFormat(8000f, // sampleRate
          8, // sampleSizeInBits
          1, // channels
          true, // signed
          false); // bigEndian
      SourceDataLine sdl;
      sdl = AudioSystem.getSourceDataLine(af);
      sdl.open(af);
      sdl.start();

      byte[] buf = new byte[1];
      int step;

      for (int j = 0; j < repeat; j++) {
        step = 10;
        for (int i = 0; i < 2000; i++) {
          buf[0] = ((i % step > 0) ? 32 : (byte) 0);

          if (i % 250 == 0)
            step += 2;
          sdl.write(buf, 0, 1);
        }
        Thread.sleep(200);
      }
      sdl.drain();
      sdl.stop();
      sdl.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

1
投票

我不知道 API 部分,但对于声音请尝试www.sounddogs.com


0
投票

Clip 界面是在您的应用程序中播放小声音的最简单方法。 这里是一个例子。如果您想播放 wav 以外的任何东西,请使用 JavaZOOM 中的 MP3SPI 或 VorbisSPI。


0
投票

好的,伙计们——这是我在等待时想到的。我以为我已经将 Stack Overflow 设置为在我的问题得到回答时通过电子邮件发送给我,所以我认为我还没有收到任何答案。于是,我一个人继续前行。 这是我发现有效的方法。

(1) 使用这个创建实例:

private PlaySounds lasershot = new PlaySounds("snd/lasershot.wav");
private PlaySounds test = new PlaySounds("snd/cash_register.au");

(2) 并创建文件 PlaySounds.java(或任何你喜欢的) 导入java.io.; 导入 javax.media.;

公共课PlaySounds { 私人播放器播放器; 私有文件文件;

   // Create a player for each of the sound files
   public PlaySounds(String filename)
   {
       file = new File(filename);
       createPlayer();
   }

   private void createPlayer()
   {
       if ( file == null )
           return;

       try 
       {
           // create a new player and add listener
           player = Manager.createPlayer( file.toURI().toURL() );
           //player.addController( (Controller) new EventHandler(player, null, null, null) );
          // player.start();  // start player
       }
       catch ( Exception e )
       {
       }
   }

    public void playSound()
    {
        // start player
        player.start();
        // Clear player
        player = null;  
        // Re-create player
        createPlayer();
    }

} // 文件结束 PlaySounds.java


(3) 要使用,请将这些插入到您想要声音的位置:

 lasershot.playSound();
 test.playSound();

这是我发现的最短/最甜蜜的。非常易于使用,并且可以播放 .au 和 .wav 文件。

非常感谢你们的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.