How to attribute sound through array and play it using Jbutton?

前端 未结 1 949
萌比男神i
萌比男神i 2021-01-27 15:23

I\'m currently working on a Magic8Ball game, very basic, although I did change it to a genie theme, and now I\'ve decided to add sound to the game.

Basically I have pre

相关标签:
1条回答
  • Simple example for playing a sound:

    import java.awt.event.*;
    import javax.swing.*;
    import javax.sound.sampled.*;
    import java.net.URL;
    import java.io.*;
    
    class SoundTest {
    
      public static void main(String[] args) throws Exception {
        URL urlToSound = new URL("file:c:/java/gun1.wav");
    //    URL urlToSound = new URL("file:c:/java/flyby1.wav");
        AudioInputStream ais = AudioSystem.getAudioInputStream(urlToSound);
        final Clip clip = AudioSystem.getClip();
        clip.open(ais);
        JButton button = new JButton("Play Sound");
        button.addActionListener( new ActionListener(){
            public void actionPerformed(ActionEvent ae) {
              clip.setFramePosition(0);
              clip.start();
            }
          } );
        JOptionPane.showMessageDialog(null, button);
      }
    }
    
    0 讨论(0)
提交回复
热议问题