Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Playing music in java  (Read 2657 times)
0 Members and 1 Guest are viewing this topic.
Offline MOEP_BIBER

Senior Newbie





« Posted 2011-06-12 19:10:58 »

Hey guys,
im currently playing music like this:

1  
2  
3  
4  
5  
6  
7  
//try
File fland = new File(game.getRoot()+"./sounds/land.wav");
Uri uri = fland.toURI();
AudioClip land = Applet.newAudioClip(uri.toURL());
//catch

land.play();


but when many audioclips come after another the game lags and the sound gets messy.

is it the right way to do it with audioclips or is there a better method?

Moep

Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-06-12 21:01:34 »

Try using the Java Sound API in javax.sound.sampled
1  
2  
3  
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(getClass().getResource("path/to/sound/file.wav")));
clip.start();


The Clip interface:
http://ra4king.is-a-geek.net/javadocs/javax/sound/sampled/Clip.html

Offline MOEP_BIBER

Senior Newbie





« Reply #2 - Posted 2011-06-13 17:17:16 »

Thx 4 the tip but i keep getting a NullPointerException and i dont know why:

1  
2  
3  
         jump = AudioSystem.getClip();
         File f = new File(myRoot + "./sounds/jump.wav");
         jump.open(AudioSystem.getAudioInputStream(getClass().getResource(myRoot + "./sounds/jump.wav")));

The exception is thrown at the third line.
The second line is just for testing and it doesnt throw an exception.

Thx 4 help

Moep
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-06-13 21:33:13 »

Try separating those calls on separate lines and then tell me which line you get the exception at.

Offline MOEP_BIBER

Senior Newbie





« Reply #4 - Posted 2011-06-13 22:09:40 »

dont know if i understood it right, but if i write it like that:

1  
2  
3  
4  
5  
         jump.open(
               AudioSystem.
               getAudioInputStream(
                     getClass().
                     getResource(myRoot + "./sounds/jump.wav")));

i get the exception at line 3.

The whole exception message is this:

java.lang.NullPointerException
   at com.sun.media.sound.WaveFileReader.getAudioInputStream(Unknown Source)
   at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
   at MusicList.init(MusicList.java:32)
   at MusicList.<init>(MusicList.java:21)
   at GameFrame.<init>(GameFrame.java:82)
   at GameApp.main(GameApp.java:7)

Moep
Offline zoto

Senior Member


Medals: 4



« Reply #5 - Posted 2011-06-13 23:24:11 »

My guess it it's something simple like your not properly pointing to the file.
Try System.out.println(myRoot + "./sounds/jump.wav"); to see if it's what you were expecting.
Just creating the File object doesn't test anything, try calling exists() or canRead() on it.
Why don't you use pass that file to getAudioInputStream, otherwise your test is irrelevant.
Offline philfrei
« Reply #6 - Posted 2011-06-14 01:50:53 »

Going back to your original post, you say there are problems with lag. Are you running the play command from its own thread? (Answer should be yes.) Are you trying to play the same cue more than once at the same time? That will probably require special handling.

Also, if two cues are recorded "efficiently" (near the loudest volume that doesn't distort), their combo (both playing at once) could be overdriving your system, and digital distortion is definitely nasty. You might look at setting their volumes to around 50% or somesuch.

[Added as an edit: using File to get a resource doesn't work so well if you ever intend to ship your app as a jar. I just went through that misunderstanding, myself: http://www.java-gaming.org/topics/obtaining-a-usable-url-getfile-from-a-resource/24342/view.html. So, I second the advice from the others here to use the getAudioInputStream method and a relative address. And yes, getting that relative address right can be a pain. An IDE can make it more confusing if you have separate source and class folders.] In my most recent build, I used the file name getAudioInputStream("sounds/jump.wav") and placed the "sounds" folder as a subfolder of the package containing the calling code.]

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-06-14 03:24:43 »

File doesn't throw an exception if the path isn't correct. It is better to check if the URL returned by getResource(String) is null. That's why you get a NullPointerException, because it returns null and getAudioInputStream throws the NPE.

Your link also doesn't look correct at all. You can't have a period in the middle of a path. You also do not need "myRoot" because you can also pass a relative path, the path being relative to you current working directory.

Offline MOEP_BIBER

Senior Newbie





« Reply #8 - Posted 2011-06-14 08:29:56 »

Ok now i found the problem.

1  
2  
         URL url = getClass().getResource(".");
         System.out.println(url.getPath());

the output is:
/C:/Users/Wolfe/Eclipse/Game%200.15/bin/

The sounds folder is in the Game 0.15 folder so i have to move up one folder via relative path.
Ive googled for 1/2 hour now and i cant seem to find it.
How is it done?

ps: its really annoying that ".." doesnt work-.-

Moep
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2011-06-14 08:43:26 »

You go up folders by using ".." (2 periods); "." (1 period) signifies the current directory

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline MOEP_BIBER

Senior Newbie





« Reply #10 - Posted 2011-06-14 09:34:19 »

1  
2  
3  
4  
5  
         System.out.println(getClass().getResource(".").getPath());
         System.out.println(getClass().getResource("./Level.class").getPath());
         System.out.println(getClass().getResource("../sounds/jump.wav").getPath());
         System.out.println(getClass().getResource(".././sounds/jump.wav").getPath());
         System.out.println(getClass().getResource("..").getPath());


only line 1 and 2 work.
what am i doing wrong?

Moep
Offline cylab

JGO Ninja


Medals: 49



« Reply #11 - Posted 2011-06-14 10:34:59 »

You can only use getResource() when loading from the classloader. Since your sound folder is not on the classpath, you can't load from there via getResource().

Usually you solve this by having a folder structure like this in your project:

1  
2  
3  
4  
5  
6  
7  
8  
src/
   [your projects classes]
resources/
   sound/
      [your projects sounds]
   images/
      [your projects images]
   whatever/


and add the resources folder to your classpath, so you can use

1  
getClass().getResource("/sounds/jump.wav")


Notice theat the pathname now is starting with a slash, so its no relative path to your class but absolute from the root of all folders and jars on the classpath. This also means you can zip your resources into the game jar or a separate resources jar when shipping your game.

Mathias - I Know What [you] Did Last Summer!
Offline MOEP_BIBER

Senior Newbie





« Reply #12 - Posted 2011-06-14 12:39:23 »

ok i made my folderstructure like you suggested.
thx 4 everyones help!

but as soon as this works the next question pops up:

now when i e.g. jump the sound only plays the first time.
what do i have to do so the sound plays every time?

i tried something like:

1  
2  
3  
4  
5  
      if(jump.isRunning()) //jump.isActive()
     {
         jump.stop();
      }
      jump.start();


but it wont work.

anybody got ideas?
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2011-06-14 22:35:31 »

You have to set the position back at 0:
1  
jump.setMicrosecondPosition(0);

Offline MOEP_BIBER

Senior Newbie





« Reply #14 - Posted 2011-06-14 22:43:18 »

Ok thank you its working now.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #15 - Posted 2011-06-14 22:45:51 »

Glad to help Smiley

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (17 views)
2014-09-21 02:42:18

BurntPizza (13 views)
2014-09-21 01:30:30

moogie (13 views)
2014-09-21 00:26:15

UprightPath (25 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (73 views)
2014-09-10 13:57:51

TehJavaDev (96 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!