Results 1 to 2 of 2
Thread: Sound Effects and .OGG usage?
- 10-30-2012, 05:32 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
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?
- 03-02-2013, 06:28 PM #2
Member
- Join Date
- Mar 2013
- Posts
- 5
- Rep Power
- 0
Re: Sound Effects and .OGG usage?
Hello Good Sir, I have a Java Class that does it all for you!
Here it is:
The code is also on: [Moderator edit: link removed]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(); } }
To use it, replace EXPLODE, HURT, etc with sound effects and the locations!Last edited by DarrylBurke; 03-02-2013 at 08:41 PM. Reason: Removed link
Similar Threads
-
Playing Sound Effects in Java
By CuppaCoffee in forum New To JavaReplies: 3Last Post: 12-22-2012, 02:37 AM -
Problems with sound in java and USB sound card
By marblecatdog in forum New To JavaReplies: 1Last Post: 04-19-2011, 12:02 PM -
See the Policy File Effects (Applications)
By Lil_Aziz1 in forum New To JavaReplies: 3Last Post: 06-06-2010, 06:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks