Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 compressed sound in Java, how?  (Read 6526 times)
0 Members and 1 Guest are viewing this topic.
Offline zappsweden

Junior Member




Java games rock!


« Posted 2006-10-24 17:47:22 »

Is there any piece of code that I can just paste into my project to make my game read any type of compressed sound? I have code for wave that I got from the Internet which worked fine but wave files are too large.
Offline fletchergames

Senior Member





« Reply #1 - Posted 2006-10-24 20:20:05 »

You have to include a Sound Provider Interface (SPI) in your classpath.  Also, your music playing code must be set up in the "right" way or it won't work.

I wrote a tutorial on my website.  A link to the appropriate page is http://www.cowgodgames.com/articles/jgptutorial/ch01.5.php .  All the code for my tutorial is linked to from http://www.cowgodgames.com/downloads.php .

My tutorial is old, and I kind of shudder when I look at the code.  Even so, the music/sound code works.  I've improved upon it slightly since (for instance, I made looped sound effects possible by making the SoundPlayer class more like the MusicStream class), but it's essentially the same.
Offline woogley
« Reply #2 - Posted 2006-10-24 21:46:13 »

EasyOGG - works wonders for me Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zappsweden

Junior Member




Java games rock!


« Reply #3 - Posted 2006-10-25 00:43:44 »

I never got that link to EasyOgg to work  Huh
Offline woogley
« Reply #4 - Posted 2006-10-25 01:44:14 »

odd, don't know why he took it down.

anyway, I have it here on my server.
Offline Matzon

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #5 - Posted 2006-10-25 07:32:03 »

link works fine for me

Offline woogley
« Reply #6 - Posted 2006-10-25 12:35:16 »

link works fine for me

he means the download link inside the blog. which actually wasn't removed, just mistyped. http://www.cokeandcode.com/downloads/EasyOgg-0.1.zip
Offline zappsweden

Junior Member




Java games rock!


« Reply #7 - Posted 2006-11-03 13:25:36 »

I always get an exception when trying to OggClip oc= new OggClip(String);
Earlier, I used a method for playing .wav files and I am in code using the same path now and have just replaced the blahblah.wav with blahblah.ogg and create and try to create an OggClip from it.
I have the OggClip file in the same folder as the wave and they both have the same name.

This method throws IOException for me.

 public OggClip(String ref) throws IOException {
                try {
                        init(Thread.currentThread().getContextClassLoader().getResourceAsStream(ref));
                } catch (IOException e) {
                        throw new IOException("Couldn't find: "+ref);
                }
        }
Offline woogley
« Reply #8 - Posted 2006-11-03 13:48:12 »

i doubt thats OggClip's fault, as its only acting upon your parameters. I'm using that same class. this is how i load the music in BeetleMania:

1  
2  
3  
4  
5  
6  
try {
   music = new OggClip(getClass().getResourceAsStream("music.ogg"));
}
catch (IOException e) {
   e.printStackTrace();
}


and it never throws an IOException at me, unless I use a bad parameter, of course. what IOException message are you getting?

notice I'm using the OggClip that takes the InputStream parameter, not the one that takes a String parameter. not that it should make a difference, as the String-parameter constructor grabs a stream anyway
Offline zappsweden

Junior Member




Java games rock!


« Reply #9 - Posted 2006-11-03 14:46:57 »

i tried using new OggClip("myfile.ogg")
or new OggClip("C:/myclip.ogg)
or the one where i use a path defined inside my game like

path=ceaw.getSoundPath();

String s=path+"myfile.ogg";
and then new MyClip(s);

Nothing works, but the paths work for the myclip.wav that i used in a method that was playing wave sound.
I also pu the myclip.ogg in the same folder as the OggClip.class in case the OggClip does not take a path (does it?)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline woogley
« Reply #10 - Posted 2006-11-03 14:51:03 »

again..

what is the IOException? (the full stack trace, including the message please)
Offline zappsweden

Junior Member




Java games rock!


« Reply #11 - Posted 2006-11-03 15:47:08 »

java.io.IOException: Couldn't find input source

   at ceaw.game.OggClip._$1280(OggClip.java:87)

   at ceaw.game.OggClip.<init>(OggClip.java:63)

   at ceaw.game.OggPlayer.run(OggPlayer.java:170)


I also did a System.out.println on the ref i sent as String to the constructor and got ->
could not find: C:\ingame_music1.ogg
Offline woogley
« Reply #12 - Posted 2006-11-03 15:55:57 »

i know it sounds stupid, but that really looks like you're not providing an existing file for OggClip to load Undecided

can you open the same file in an FileInputStream? does File(path).exists() == true?
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #13 - Posted 2006-11-03 16:10:20 »

Ehm... lol. Did you notice that you print something different than you try to load?

If ref equals "C:\ingame_music1.ogg" then Thread.currentThread().getContextClassLoader().getResourceAsStream(ref) gives you null. Try printing that instead and you'll see.

That getResource* stuff loads stuff from the classpath (I assume that "C:\" isnt in the classpath).

弾幕 ☆ @mahonnaiseblog
Offline zappsweden

Junior Member




Java games rock!


« Reply #14 - Posted 2006-11-03 16:14:44 »

try
{
  FileInputStream f_in = new FileInputStream("C:/ingame_music1.ogg");
} catch(FileNotFoundException e) {System.exit(55);}

worked without any exception.
Offline woogley
« Reply #15 - Posted 2006-11-03 16:36:28 »

read oNyx's post. more or less, getResource*() loads relative paths, not absolutes. pass that FileInputStream to OggClip and it will work:

1  
2  
3  
4  
5  
6  
try {
   clip = new OggClip(new FileInputStream("C:/ingame_music1.ogg"));
}
catch (IOException e) {
   e.printStackTrace();
}
Offline zappsweden

Junior Member




Java games rock!


« Reply #16 - Posted 2006-11-03 17:14:08 »

Yes, seems the simplest way was to use the InputStream instead.it is now working Smiley
The only thing missing in that OggPlayer is that I would want to know how long a sound file is, so if i put several different long OGG's in queue, i can start the second one when the first one is finished.
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #17 - Posted 2006-11-03 18:32:36 »

The source is provided. Rather than knowing the length of the OGG and attempting to sync something in your main loop it's probably easy to just poll whether clip has stopped playing.

1  
2  
3  
   public boolean stopped() {
      return player == null;
   }


Might work.

Kev

Offline zappsweden

Junior Member




Java games rock!


« Reply #18 - Posted 2006-11-04 00:46:20 »

The source is provided. Rather than knowing the length of the OGG and attempting to sync something in your main loop it's probably easy to just poll whether clip has stopped playing.

1  
2  
3  
   public boolean stopped() {
      return player == null;
   }


Might work.

Kev

I tried it but it did not work. I think threads do not become null when they die. However, this code worked for me and I use it withing my SoundPlayer loop.

code:

 public boolean playing() {
                if((player != null) && (player.isAlive())) return true;
                else return false;
        }




Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #19 - Posted 2006-11-04 01:01:44 »

[...]getResource*() loads relative paths, not absolutes.[...]

Sort...of. With a leading slash its kinda absolute... from all root dirs (virtual or not) in the classpath. So if your jar contains gfx/foo.tga you can access it from foo.bar.Game (or anywhere else) via getResourceAsStream("/gfx/foo.tga").

I prefer using a leading slash, because its imo clearer.

弾幕 ☆ @mahonnaiseblog
Offline zappsweden

Junior Member




Java games rock!


« Reply #20 - Posted 2006-11-04 01:34:47 »

The only problem remaining is, Borland JBuilder seems to delete my imported class files that use to make OGG work, odd. Since most of the easyogg files are only class and not source i cannot compile them inside JBuilder pfffftttt.

I have to compile it and then run it with some other software (exe4j for instance).
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.

pw (25 views)
2014-07-24 01:59:36

Riven (25 views)
2014-07-23 21:16:32

Riven (19 views)
2014-07-23 21:07:15

Riven (22 views)
2014-07-23 20:56:16

ctomni231 (51 views)
2014-07-18 06:55:21

Zero Volt (46 views)
2014-07-17 23:47:54

danieldean (37 views)
2014-07-17 23:41:23

MustardPeter (40 views)
2014-07-16 23:30:00

Cero (56 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!