Need a little help with sound in a JApplet

Posted by jacob schuschel on Stack Overflow See other posts from Stack Overflow or by jacob schuschel
Published on 2009-11-16T23:37:42Z Indexed on 2010/06/06 1:02 UTC
Read the original article Hit count: 511

Filed under:
|

I am working on a solitaire game in Java, and i need to implement sound when the desk is shuffled, card flipped, etc. I used the following sites as reference to try and get it to work, but i am getting Null Pointer Exceptions or mishandled URL exception (depending on what i tweak).
here
Also, i am using netbeans 6.7.1 as my IDE.
I will try to break down the code and explain:


package cardgame;

import java.applet.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;
import javax.swing.*;
import java.io.*;
import java.net.*;

/**
 *
 * @author jacob
 */
public class Sound extends JApplet {

    private AudioClip song; // Sound player
    private String URL = null;
    private URL songPath; // Sound path
    /*
     *sound_1 =  shuffling cards
     *sound_2 = to discard
     *sound_3 =  from discard
     *sound_4 = cardflip 1
     *sound_5 = cardflip 2
     */

    Sound(String filename) {
        try {
           songPath = new URL(getCodeBase(),filename); // Get the Sound URL
        } catch (MalformedURLException ex) {
            Logger.getLogger(Sound.class.getName()).log(Level.SEVERE, null, ex);
        }
        song = getAudioClip(songPath); // Load the Sound
    }
    Sound(int i) {
        URL = "./sounds/sound_" + i + ".wav";
        System.out.println(URL);
        try {
            songPath = new URL(URL); // Get the Sound URL
            song = getAudioClip(songPath);
        } catch (MalformedURLException ex) {
            Logger.getLogger(Sound.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void playSound() {
        song.loop(); // Play
    }

    public void stopSound() {
        song.stop(); // Stop
    }

    public void playSoundOnce() {
        song.play(); // Play only once
    }
}

    

The 2 different construcors are for different ways i tried to implement this. The first one creates the filepath, and passes it in. The second one builds the filepath in the constructor, given a sound # (i made a list of what numbers correspond to what sound for reference). I am getting the followig errors out:

./sounds/sound_1.wav
Nov 16, 2009 4:14:13 PM cardgame.Sound 
./sounds/sound_2.wav
SEVERE: null
java.net.MalformedURLException: no protocol: ./sounds/sound_1.wav
./sounds/sound_3.wav
        at java.net.URL.(URL.java:583)
        at java.net.URL.(URL.java:480)
        at java.net.URL.(URL.java:429)
./sounds/sound_4.wav
./sounds/sound_5.wav
        at cardgame.Sound.(Sound.java:46)
        at cardgame.Game.loadSounds(Game.java:712)
        at cardgame.Game.(Game.java:62)
        at cardgame.Main.main(Main.java:25)
Nov 16, 2009 4:14:13 PM cardgame.Sound 
SEVERE: null
java.net.MalformedURLException: no protocol: ./sounds/sound_2.wav
        at java.net.URL.(URL.java:583)
        at java.net.URL.(URL.java:480)
        at java.net.URL.(URL.java:429)
        at cardgame.Sound.(Sound.java:46)
        at cardgame.Game.loadSounds(Game.java:712)
        at cardgame.Game.(Game.java:62)
        at cardgame.Main.main(Main.java:25)
Nov 16, 2009 4:14:13 PM cardgame.Sound 
SEVERE: null
java.net.MalformedURLException: no protocol: ./sounds/sound_3.wav
        at java.net.URL.(URL.java:583)
        at java.net.URL.(URL.java:480)
        at java.net.URL.(URL.java:429)
        at cardgame.Sound.(Sound.java:46)
        at cardgame.Game.loadSounds(Game.java:712)
        at cardgame.Game.(Game.java:62)
        at cardgame.Main.main(Main.java:25)
Nov 16, 2009 4:14:13 PM cardgame.Sound 
SEVERE: null
java.net.MalformedURLException: no protocol: ./sounds/sound_4.wav
        at java.net.URL.(URL.java:583)
        at java.net.URL.(URL.java:480)
        at java.net.URL.(URL.java:429)
        at cardgame.Sound.(Sound.java:46)
        at cardgame.Game.loadSounds(Game.java:712)
        at cardgame.Game.(Game.java:62)
        at cardgame.Main.main(Main.java:25)
Nov 16, 2009 4:14:13 PM cardgame.Sound 
SEVERE: null
java.net.MalformedURLException: no protocol: ./sounds/sound_5.wav
        at java.net.URL.(URL.java:583)
        at java.net.URL.(URL.java:480)
        at java.net.URL.(URL.java:429)
        at cardgame.Sound.(Sound.java:46)
        at cardgame.Game.loadSounds(Game.java:712)
        at cardgame.Game.(Game.java:62)
        at cardgame.Main.main(Main.java:25)

    

Thanks for those who read and more thanks to those who help. I know it is somewhat long, but i would rather get it all out there, than have 50 questions that come back or have people not answer due to lack of initial info. also only lets me post a single link right now, so the links are given below

dreamincode.net/forums/showtopic14083.htm
stackoverflow.com/questions/512436/java-playing-wav-sounds
deitel.com/articles/java_tutorials/20060422/LoadingPlayingAudioClips/index.html

© Stack Overflow or respective owner

Related posts about java

Related posts about sound