java - 基于 Java 的游戏的声音格式

标签 java audio file-format

我正在开发一款 Java 游戏,想要捆绑和播放大量采样音效。我打算使用标准的 javax.sound.sampled 功能来播放它们。

哪种格式最适合这类样本?我正在寻找在 Java 中以编程方式使用的良好压缩、良好质量和便利性的结合。

最佳答案

质量与尺寸

这里有一些关于 MP3 与 WAV 区别的有趣文章。

为什么是 MP3 和 WAV?这些是我在创建程序时最常看到的,因此兼容性从来都不是问题。

我有这个非常有用的 java 文件,可以为您完成一切。您使用声音文件的路径构造对象,然后使用方法播放、停止、循环等。

与此同时,您可以查看这些以供引用,尽管它们不那么干净:How can I play sound in 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 here .请参阅文档 here .

关于java - 基于 Java 的游戏的声音格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3237498/

相关文章:

java - 在 Ant build.properties 中添加环境变量

java - 使用 'OPE' =? 等语法时,字符串到数字的转换失败

iphone - iPhone中的插孔/听筒检测

java - 如何捕获Java中的视频和音频?

python - 有ELF的PEFile吗

支持多流的音频文件格式

java - 无法打印 GC 详细信息

c# - 在 C# 中播放声音 byte[]

image - 这个 EMF 图像 A4 大小如何?

java - 如果文本的最大行数超过 4 条,如何显示阅读更多文本?