Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Programming JavaGame - Help Please

  • 10-04-2011 7:12pm
    #1
    Registered Users, Registered Users 2 Posts: 108 ✭✭


    I am currently trying to programme a bank robbing game but I cant seem to get the sound working and could do with a little help. I have checked online for some pointers but havent found anything of interest yet...


    Bare in mind that I have only started trying to learn java so I dont have much of a clue atm tongue.gif

    This is the code I am using atm:

    import java.io.*;
    import java.net.URL;
    import javax.sound.sampled.*;

    public enum SoundEffect {
    SHOOT("ar.wav"), // shooting
    WALK("footsteps.wav"), // gong
    MUSIC("antichrist.mp3"); // bullet

    // Nested class for specifying volume
    public static enum Volume {
    MUTE, LOW, MEDIUM, HIGH
    }

    public static Volume volume = Volume.LOW;

    // Each sound effect has its own clip, loaded with its own sound file.
    private Clip clip;

    // Constructor to construct each element of the enum with its own sound file.
    SoundEffect(String soundFileName) {
    try {
    // Use URL (instead of File) to read from disk and JAR.
    URL url = this.getClass().getClassLoader().getResource(soundFileName);
    // Set up an audio input stream piped from the sound file.
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
    // Get a clip resource.
    clip = AudioSystem.getClip();
    // Open audio clip and load samples from the audio input stream.
    clip.open(audioInputStream);
    } catch (UnsupportedAudioFileException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (LineUnavailableException e) {
    e.printStackTrace();
    }
    }

    // Play or Re-play the sound effect from the beginning, by rewinding.
    public void play() {
    if (volume != Volume.MUTE) {
    if (clip.isRunning())
    clip.stop(); // Stop the player if it is still running
    clip.setFramePosition(0); // rewind to the beginning
    clip.start(); // Start playing
    }
    }

    // Optional static method to pre-load all the sound files.
    static void init() {
    values(); // calls the constructor for all the elements
    }
    }



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

    // Testing the SoundEffect enum in a Swing application
    public class SoundEffectDemo extends JFrame {

    // Constructor
    public SoundEffectDemo() {
    // Pre-load all the sound files
    SoundEffect.init();
    SoundEffect.volume = SoundEffect.Volume.LOW; // un-mute

    // Set up UI components
    Container cp = this.getContentPane();
    cp.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
    JButton btnSound1 = new JButton("Sound 1");
    btnSound1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    SoundEffect.EXPLODE.play();
    }
    });
    cp.add(btnSound1);
    JButton btnSound2 = new JButton("Sound 2");
    btnSound2.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    SoundEffect.GONG.play();
    }
    });
    cp.add(btnSound2);
    JButton btnSound3 = new JButton("Sound 3");
    btnSound3.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    SoundEffect.SHOOT.play();
    }
    });
    cp.add(btnSound3);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Test SoundEffct");
    this.pack();
    this.setVisible(true);
    }

    public static void main(String[] args) {
    new SoundEffectDemo();
    }
    }


    Any useful comments would be much appreciated.


Advertisement