开发者

Java game sounds in applications (as in, NOT applets)

开发者 https://www.devze.com 2023-01-10 11:03 出处:网络
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

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 开发者_C百科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!


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

See Produce special 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();
    }
  }
}


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消