应用程序中的 Java 游戏声音(例如,而不是小程序)

发布于 2024-09-12 17:38:43 字数 253 浏览 7 评论 0原文

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

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

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

I have two questions - (1) How to play small sound clips, e.g. saucer flying, bullets shooting, things getting hit with bullets, etc. Very short, but real-time, sounds. I like the old arcade sounds, so they need not be largess .wav's. I want to run these in as little code as possible. Which leads to my second question...
(2) Does anyone know where to find these sound clips.

A little note, I've seen some of the answers here, and they seem incomplete. If you have a straight, generic code, that's great! I know very little about sounds as I don't normally get this far in my game writing.

Thanks for any and all information - I do appreciate it!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

后来的我们 2024-09-19 17:38:43

使用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();
    }
  }
}

Using javax.sound, it's possible to have simple sound effect.


EDIT: Using a Thread (or not)

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();
    }
  }
}
葬心 2024-09-19 17:38:43

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

I don't know about the API part, but for the sounds try www.sounddogs.com

陌上青苔 2024-09-19 17:38:43

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

Clip interface is the easiest way to play small sounds in your app. Here is an example. If you want to play anything other than wav, use MP3SPI or VorbisSPI from JavaZOOM.

时光磨忆 2024-09-19 17:38:43

好吧,伙计们——这就是我在等待时想到的。我以为我已经将 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 文件。

我非常感谢你们所有的帮助。

Okay guys - here's what i came up with while I was waiting. I thought I had set the Stack Overflow settings to email me when my question was answered, so I thought I received no answers yet. Thus, I continued on by myself.
Here's what I found that worked.

(1) Create the instance by using this:

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

(2) And create the file PlaySounds.java (or whatever you like)
import java.io.;
import javax.media.
;

public class PlaySounds
{
private Player player;
private File file;

   // 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();
    }

} // End of file PlaySounds.java


(3) To use, insert these where you want the sounds:

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

This was the shortest / sweetest I found. Very easy to use, and plays both .au AND .wav files.

I do appreciate all your help guys.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文