Jump to content
  • Advertisement
Sign in to follow this  
H_o_p_s

[java] A better Sound_Manager class

This topic is 4900 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is my current Sound_Manager Class
import java.applet.*;
import java.io.*;

public class Sound_Manager extends java.lang.Thread {

    private Applet a;
    private AudioClip onceClip;
    private AudioClip loopClip;
    private boolean looping;
    
    public JavaTactics j; //HERE FOR REFERENCES TO LOG
    
    public Sound_Manager(JavaTactics j){
	    this.j = j;
	    j.log.add("Created Sound_Manager...");
    }
 
    public void play(String playFile){
        File file = new File(playFile);
        try{
            onceClip = a.newAudioClip(file.toURL());
            onceClip.play();       
        }
        catch(Exception e){
            j.log.add("Error: "+e);
        }
   }

    public void loop(String playFile){
	    stopLoop();
		    File file = new File(playFile);
		    try{
			    loopClip = a.newAudioClip(file.toURL());
			    loopClip.loop();       
		    }
		    catch(Exception e){
			    j.log.add("Error: "+e);
		    }
		    looping = true;
   }
        
    public void stopLoop() {
	if (looping)
		loopClip.stop();
	looping = false;
    }
}

Now, there are a couple problems with this.
  • No support for mp3/ogg, only midi and wav
  • There is significant time to load a file to play
  • Not enough control. If I wanted to start the file at 2 minutes 13 seconds etc...
Now... if I used a library for ogg playback, is there still the wait time for loading the file from disk? If I have to cache it at program launch, that is okay also... So any ideas here?? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
If you were to use something like OpenAL for Java, you could 'stream' an OGG file from disk with no delay at all. I have stream a 100MB ogg with no problem at all. You can also stream .wavs if you really wanted to, but .ogg is the perferred since it is compressed like MP3.

With OpenAL and OGG, you can 'seek' to whatever time you want as well. It is a simple one function call, well 2 total just to make sure it is seekable, and that's it. You have full control of where it starts, and you can alwasy get the time it is currently at.

As for file type support - technicaly - it will support any audio format, all you have to do is have a decoder that can decompress that format into PCM (.wav) data so OpenAL can use it. That's it.

You alwasy have the option to load the .OGG/.WAV/etc.. into memory, but as you know it takes a lot of system memory as well as time loading. This option is good though for very small sound fx that would be pointless to stream - but you could if you wanted to.

Quote:
Now, there are a couple problems with this.

Yeap, it's only 40 lines! My audio library is 2,462, and that's without comments [grin][lol] Seriously though, expect a bit more code if you use OpenAL + OGG. I do not know if OGG is avaliable on Java though, so you will need to look into that. Glad to see you progressing so well with your game [smile]

- Drew

Share this post


Link to post
Share on other sites
Quote:
Original post by H_o_p_s
No support for mp3/ogg, only midi and wav

Here is an article I found awhile back on adding simple .mp3 support to Java. All you're really doing is extending the javax.sound media package. However, here is someone's added .mp3 support, perhaps that might help. As for .ogg support, there is JOrbis.

Quote:
There is significant time to load a file to play

This is because you are loading each clip one at a time. The sound manager itself doesn't need to be in a separate thread, you just have to ensure that the audio is played in one (e.g. you could create an SoundPlayer or something). As per this tutorial, Java's threads make asynchronous loading a viable (and in cases like these it's necessary) option. The simple classes presented in that tutorial perform the same function as java.awt.image.MediaTracker. Even though you are using an applet, you could also investigate the sun.audio.AudioStream class. To prevent further linkage (I'm on a roll), here is a good list of articles that might help (several already linked to).

Quote:
Not enough control. If I wanted to start the file at 2 minutes 13 seconds etc...

You probably want to plow through the javax.sound.sampled tutorials. They seem to support what you want, though I'm not entirely sure.

Lastly, on the subject of OpenAL support, there appear to only be bindings to the C++ OpenAL library (which is unfortunate).

Share this post


Link to post
Share on other sites
Simple explanation: Use clips when the files you're trying to play are small
use Streams and apply threading when playing huge files...

Further note that Java 5 is more applicable for this purpose since they
resolved a lot of audio issues compared to Java 1.4.x

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!