Results 1 to 2 of 2
  1. #1
    JagFig is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Sound Effects and .OGG usage?

    I am currently working on a video game for my Java class, and I have hit a huge wall. I have no idea how to use audio in Java, and everything I have read online does not seem to work.

    I really do not want to resort to using .mp3 files for my audio, but I guess learning how to use them may help me understand how to use .ogg files as well.
    But now for the question:

    Does anyone have a good method of playing sound effects and music that doesn't sound horrendous?

  2. #2
    InfiniteCoder is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default Re: Sound Effects and .OGG usage?

    Hello Good Sir, I have a Java Class that does it all for you!

    Here it is:

    Java Code:
    import java.io.*;
    import java.net.URL;
    import javax.sound.sampled.*;
    
    public enum SoundEffects {
       EXPLODE("explode.wav"),
       HURT("hurt.wav"),
       PICKUP("pickup.wav"),
       POWERUP("powerup.wav"),
       BREAK("break.wav"),
       DISK_ONE("music/disk1.wav");
    
       public static enum Volume {
          MUTE, LOW, MEDIUM, HIGH
       }
    
       public static Volume volume = Volume.LOW;
    
       private Clip clip;
       private boolean isMusic = false;
    
       SoundEffects(String soundFileName) {
    	   if(soundFileName.toLowerCase().startsWith("disk")) {
    		   isMusic = true;
    	   }else {
    		   isMusic = false;
    	   }
          try {
             URL url = this.getClass().getClassLoader().getResource("sounds/" + soundFileName);
             AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
             clip = AudioSystem.getClip();
             clip.open(audioInputStream);
          } catch (UnsupportedAudioFileException e) {
             e.printStackTrace();
          } catch (IOException e) {
             e.printStackTrace();
          } catch (LineUnavailableException e) {
             e.printStackTrace();
          }
       }
    
       public void play() {
          if (volume != Volume.MUTE) {
             if (clip.isRunning())
                clip.stop();
             clip.setFramePosition(0);
             clip.start();
          }
       }
    
       static void init() {
          values();
       }
    }
    The code is also on: [Moderator edit: link removed]

    To use it, replace EXPLODE, HURT, etc with sound effects and the locations!
    Last edited by DarrylBurke; 03-02-2013 at 09:41 PM. Reason: Removed link

Similar Threads

  1. Playing Sound Effects in Java
    By CuppaCoffee in forum New To Java
    Replies: 3
    Last Post: 12-22-2012, 03:37 AM
  2. Problems with sound in java and USB sound card
    By marblecatdog in forum New To Java
    Replies: 1
    Last Post: 04-19-2011, 01:02 PM
  3. See the Policy File Effects (Applications)
    By Lil_Aziz1 in forum New To Java
    Replies: 3
    Last Post: 06-06-2010, 07:11 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •