Sound format for Java games

I am developing a Java game and want to combine and play a number of sound effects. I plan to use the standard javax.sound.sampled functions to play them.

Which format is best for these samples? I am looking for a combination of good compression, decent quality and ease of use for programmatic use in Java.

+3
source share
3 answers

Quality and size

Here are some interesting articles about the differences between MP3 and WAV.

MP3 WAV? , , .

Java , . , , , ..

, : Java?.


, , . javax.swing, , , , .

, , 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);
    }

}

:

String projectPath = <project directory>; // getting this is another question    
Sound helloSound = new Sound(new File(projectPath + "/Sounds"));

helloSound.play(); , , .

, , . , . .


,

MP3 , . , .

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();

    }

}

, , ?;.)

jar . . .

+3

Java mp3, mp3 , .

java MP3-, http://www.javazoom.net/javalayer/javalayer.html, javax.sound. Sun mp3- java-.

+2

, Sun .au. , . - Java Media Framework.

+2

Source: https://habr.com/ru/post/1754414/


All Articles