SOUND

how do i add sound in to a java game not a aplet a GAME !!!

Applets and games are two different things :wink:
Applets are how you display what you make in Java. Games are an example of what you make in Java.
Anyways, from what I’ve heard java sound is hard to work with so check out TinySound.

NO I NEED IT NOW I DONT WANT LIBS I AM WORKING ON A LUDUM DARE GAME

Maybe you should of thought this out before hand.

Besides, TinySound seems to be pretty much plug and play in less then 5 minutes, certainly better than figuring out Java Sound.

package net.kemoy.gui;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class Sound {
	public static Sound music = loadSound("/music.wav");
	public static Sound break_tile = loadSound("/break.wav");
	public static Sound pickup = loadSound("/points.wav");
	
	private Clip clip;

	public static Sound loadSound(String fileName) {
		Sound sound = new Sound();
		try {
			AudioInputStream ais = AudioSystem.getAudioInputStream(Sound.class
					.getResource(fileName));
			Clip clip = AudioSystem.getClip();
			clip.open(ais);
			sound.clip = clip;
		} catch (Exception e) {
			System.out.println(e);
		}
		return sound;
	}

	public void loop(final int delta) {
		try {
			if (clip != null)
				new Thread() {
					public void run() {
						synchronized (clip) {
							clip.stop();
							clip.setFramePosition(0);
							clip.loop(delta);
						}
					}
				}.start();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}

	public void play() {
		try {
			if (clip != null)
				new Thread() {
					public void run() {
						synchronized (clip) {
							clip.stop();
							try {
								Thread.sleep(45);
								clip.setFramePosition(0);
								clip.start();
							} catch (InterruptedException e) {}
						}
					}
				}.start();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}
}

This will work fine as long as you don’t have too many sounds going on at once.

i get this error

java.lang.NullPointerException
	at com.sun.media.sound.StandardMidiFileReader.getSequence(Unknown Source)
	at javax.sound.midi.MidiSystem.getSequence(Unknown Source)
	at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
	at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
	at Sound.loadSound(Sound.java:14)
	at Sound.<clinit>(Sound.java:7)
	at Main.<init>(Main.java:42)
	at Main.main(Main.java:153)

With what code…

kpars’s code !

That means the file is not found.

Try messing with the separators, so instead of “/res/music.wav” it would be “res/music.wav”, or vise-verse.

THANK YOU SO MUCH :smiley: :smiley: :D: :smiley: :smiley:
I dont know how to thank you :smiley:

Im going to say thank you aswell ive been searching for this all day.excellent work my friend excellent.