Trying to play background music in my game

Hello,

 I've been working on a new little game project and I could use some help. I've been trying to put background music into my game, but when I run this code 

//Background music
		try
		{
		Sequence sequence = MidiSystem.getSequence(new FileInputStream("bgm to try.mid")); 
		Sequencer sequencer = MidiSystem.getSequencer();
		sequencer.open();
		sequencer.setSequence(sequence);
		sequencer.start();
		}
		catch(FileNotFoundException e)
		{
			System.out.println("Sound Problem1");
		}
		catch(MidiUnavailableException e)
		{
			System.out.println("Sound Problem2");
		}
		catch(InvalidMidiDataException e)
		{
			System.out.println("Sound Problem3");
		}
		catch(IOException e)
		{
			System.out.println("Sound Problem4");
		}

in main, Sound Problem1 is printed out to standard output and the program works fine, but no sound is played. I’ve had a lot of trouble with file input streams in the past, and I didn’t have any file input code other than that, but I’m honestly not sure how to fix this code or other alternative to explore. The file name appears to be correct and it’s in my source folder in eclipse, so I’m not sure if that’s the problem. Can anyone here provide some advice? I would really appreciate any help.

Thanks!

[quote]it’s in my source folder in eclipse
[/quote]
That’s probably it, assuming you mean:

Project src bgm to try.mid

It should be in the same level as the source folder:

Project src bgm to try.mid

Also you could probably use a File instead of a FileStream, if it keeps giving you trouble, that way you can also print what path it’s trying to find (debugging stage) and easily test if the file doesn’t exist.

Hey! That worked!

Hmm… It’s not looping though. Is there a simple way I could make the song loop with my current code?

sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);

Consult the JavaDocs for this kind of stuff.

Fair that, I looked at stack overflow’s documentation instead XD sorry about that.

As a tip: Don’t rely on Stack Overflow too much.

They contain common questions on problems that aren’t obvious to the programmers. I used to rely on them so much, to the point that my project is starting to break without me knowing what to do to fix it. And sometimes, I can get the Wikipedia syndrome from clicking on too many questions.

Also see when you are handling your exceptions?

Those print statements might make sense now, to you and only you. Use the exception given to you.


e.printStackTrace();

// or

e.printStackTrace("insert your info here");

Printing the stack will give you the info needed, of it can’t find the file it will say it can’t find xx file in yy directory on line ii.

As you can imagine this would be a beneficial if you had 300 different sound effects and 15 different music.

[quote]Sound Problem1 is printed out to standard output and the program works fine
[/quote]
This is because you are handling the exception yourself, you are basically telling the program when it compiles “it’s ok you tried, I caught the error and handled it myself, it is safe to run”.

Try/Catch blocks are handled exceptions, they are designed to catch the error and fix it or terminate the program in a way that you can debug the error. If you wanted to kill the program due to one of these exceptions, you need to tell it so by throwing a RunTimeException.