Problem loading music with TinySound library

I have a problem loading the music with TinySound library.
I usually load every image like “res/…/fileName.ext”, but now I’ve no idea what I’m missing…


        TinySound.init();
        Music music = TinySound.loadMusic("res/music_tracks/Rayman_2_Cut.ogg");
        music.play(true);

Output:

Unable to find resource /res/music_tracks/Rayman_2_Cut.ogg!
Exception in thread "main" java.lang.NullPointerException
	at poker.state.StartScreenState.init(StartScreenState.java:45)

Have you double checked that the path is EXACTLY the same? How are your files located?

Yes! This link “res/music_tracks/Rayman_2_Cut.ogg”, worked fine for Slick2D Music.

[quote]How are your files located?
[/quote]

  • Documents
    • NetBeansProjects
      • Rayman2D
        • nbproject
        • src
        • res
          • gui
          • music_tracks
            (.ogg files)

But not for this library… Any one ?

I have noticed a similar problem when I´m trying to add new .png files in Eclipse. Sometimes, no matter how much I update, I´m getting an error that the file can´t be found. Then I have to open my .png files in the text editor in Eclipse and just add and delete a character, just so I can save it again. After that it works. It´s silly, really, but no one seems to know what to do about this problem so I guess I´ll just continue doing it. :slight_smile:

If you are using Eclipse you can try it. Then at least you can rule out if that is the problem…

Here is how I load my files:

Audio.storeSound("hit_shield", TinySound.loadSound(Audio.class.getResource("/audio/hit_shield.wav")));
		Audio.storeSound("shoot", TinySound.loadSound(Audio.class.getResource("/audio/shoot.wav")));
		Audio.storeSound("alien_death", TinySound.loadSound(Audio.class.getResource("/audio/alien_death.wav")));
		Audio.storeSound("player_hit", TinySound.loadSound(Audio.class.getResource("/audio/player_hit.wav")));
		Audio.storeSound("player_upgrade", TinySound.loadSound(Audio.class.getResource("/audio/player_upgrade.wav")));

Just do / isntead of /res/ I think.

Edit:
For the guy above me. Did you right click on your res folder and hit refresh after adding files/editing them?

That did not fix the problem…
Output:


Unable to find resource /music_tracks/Rayman_2_Cut.ogg!
Exception in thread "main" java.lang.NullPointerException

Tell me, from which library did u pull Audio class and what are u doing with it after loading the sound?

Audio is one of my personal utility classes that I use to control TinySound with.

package utilities;

/**
 * This class allows a quick way to utilize TinySound to play music and sound clips.
 * 
 * @author Valkryst
 * --- Last Edit 23-Sep-2013
 */
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import kuusisto.tinysound.Music;
import kuusisto.tinysound.Sound;
import kuusisto.tinysound.TinySound;

public class Audio {
	public static boolean isDirectionalSoundOn = true;
	private static HashMap<String, Sound> storedSounds = new HashMap<String, Sound>();
    private static Music music; // The music file that is currently being played.

    /**
     * Prepares TinySound for use.
     */
    public static void initialize() {
    	TinySound.init();
    }
    
    /**
     * This method plays a music file.
     * @param path The path to the music file that is going to be played.
     * @param loop Whether the music file should be played on loop or not.
     */
    public static void playMusic(final String path, final boolean loop) {
		music = TinySound.loadMusic(Audio.class.getResource(path));
		
		if(loop) {
		    music.loop();
		    music.play(true);
		}
    }

    /**
     * Set the volume of the music file that is currently being played.
     * @param volume The volume level that the currently playing music file should be played at.
     */
    public static void setVolume(final double volume) {
    	music.setVolume(volume);
    }
    
    /**
     * Set the pan of the music file that is currently being played.
     * @param pan The pan that the currently playing music file should use.
     */
    public static void setPan(final double pan) {
    	music.setPan(pan);
    }

    /**
     * Stops the currently playing music file.
     */
    public static void stopMusic() {
    	music.stop();
    }
    
    /**
     * Pauses the currently playing music file.
     */
    public static void pauseMusic() {
    	music.pause();
    }
    
    /**
     * Resumes the currently playing music file.
     */
    public static void resumeMusic() {
    	music.resume();
    }
    
    /**
     * Stored the specified sound into a hashmap for use without reloading the file every use.
     * @param nameIn The name identifier of the sound to store.
     * @param soundIn The sound to store.
     */
    public static void storeSound(final String nameIn, final Sound soundIn) {
    	storedSounds.put(nameIn, soundIn);
    }
    
    /**
     * This method plays a stored sound file.
     * @param nameIn The name identifier of the stored sound to play.
     * @param volumeIn The volume at which to play the stored sound.
     * @param xCoordIn the x coordinate of the object creating the
     */
    public static void playStoredSound(final String nameIn, final double volumeIn, final int xCoordIn) {
    	if(isDirectionalSoundOn) {
    		double pan = -1;
    		
	    	if(xCoordIn >= 768) {
	    		pan = 1;
	    	} else if(xCoordIn >= 512) {
	    		pan = 0.66;
	    	} else if(xCoordIn >= 256) {
	    		pan = 0.33;
	    	} else {
	    		pan = 0;
	    	}
	    	
	    	storedSounds.get(nameIn).play(volumeIn, pan);
    	} else {
    		storedSounds.get(nameIn).play(volumeIn);
    	}
    }
    
    /**
     * This method plays a sound file.
     * @param path The path to the music file that is going to be played.
     * @param loop Whether the music file should be played on loop or not.
     * @param loops The number of times to play the sound file.
     * @param volumeIn The volume at which to play the stored sound.
     */
    public static void playSound(final String path, final boolean loop, final int loops, final double volumeIn) {
		Sound sound = TinySound.loadSound(Audio.class.getResource(path));
		
		if (loop) {
		    for (int i = 0; i < loops; i++) {
				sound.play();
				try {
				    Thread.sleep(1000);
				} catch (InterruptedException e) {
					Logger logger = Logger.getLogger(Audio.class.getName());
					logger.log(Level.SEVERE, e.toString(), e);
				}
		    }
		} else {
		    sound.play(volumeIn);
		}
    }

    /**
     * Shuts down TinySound.
     */
    public static void shutdown() {
    	TinySound.shutdown();
    }
}

Derp, just noticed that you’re using Netbeans. Can’t help there, I’m using eclipse and the code I showed you works fine.