Sound format of game based on Java
I'm developing a java game that wants to bundle and play some sampled sound effects I'm going to use standard javax sound. The sampled function to play them
Which format is most suitable for such samples? I am looking for a mix of good compression, good quality and easy Java programming
Solution
Mass and size
Here are some interesting articles about the differences between MP3 and wav
> http://www.angelfire.com/vt2/tommymc3/MP3vsWav.html > http://www.scinet.cc/articles/wma-vs-mp3/wma-mp3.html > http://ask.yahoo.com/20000602.html
Why MP3 and wav? These are the most common when I create programs, so compatibility is never a problem
I have this very useful java file that can do everything for you You use the path of the sound file to construct the object, and then use methods to play, stop, cycle, etc
In the meantime, you can look at these for reference, although they are not so clean: how can I play sound in Java
Simple programming
Unfortunately, I don't have the files on this computer, but this is a simplified version This doesn't use javax as you want Swing, but to be honest, I always like alternative methods
Because this is different from my files on other computers, I can't guarantee compatibility with MP3
import sun.audio.*; import java.io.*; public class Sound { private InputStream input; private AudioStream audio; public Sound (File fileName) { input = new FileInputStream(); audio = new AudioStream(input); } public void play() { AudioPlayer.player.start(audio); } public void stop() { AudioPlayer.player.stop(audio); } }
example:
String projectPath = <project directory>; // getting this is another question Sound helloSound = new Sound(new File(projectPath + "/Sounds"));
Now you can call hellosound play(); Whenever you want to play a clip
I prefer this method, so you don't have to stream everything every time you want to play the clip This should effectively occlude and blur some sounds, but do not overuse it and occupy memory For common sound effects
Simple programming, continued
Find a good file to play MP3, as I demonstrated above I completely forgot to put my stuff in a separate thread, so it's a better choice
import java.io.BufferedInputStream; import java.io.FileInputStream; import javazoom.jl.player.Player; public class MP3 { private String filename; private Player player; // constructor that takes the name of an MP3 file public MP3(String filename) { this.filename = filename; } public void close() { if (player != null) player.close(); } // play the MP3 file to the sound card public void play() { try { FileInputStream fis = new FileInputStream(filename); BufferedInputStream bis = new BufferedInputStream(fis); player = new Player(bis); } catch (Exception e) { System.out.println("Problem playing file " + filename); System.out.println(e); } // run in new thread to play in background new Thread() { public void run() { try { player.play(); } catch (Exception e) { System.out.println(e); } } }.start(); } // test client public static void main(String[] args) { String filename = args[0]; MP3 mp3 = new MP3(filename); mp3.play(); // do whatever computation you like,while music plays int N = 4000; double sum = 0.0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { sum += Math.sin(i + j); } } System.out.println(sum); // when the computation is done,stop playing it mp3.close(); // play from the beginning mp3 = new MP3(filename); mp3.play(); } }
Pie is simple, isn't it
Get jar here See the document here