基于 Java 的游戏的声音格式

发布于 2024-09-08 22:30:52 字数 130 浏览 4 评论 0原文

我正在开发一个 Java 游戏,想要捆绑并播放许多采样的音效。我计划使用标准 javax.sound.sampled 功能来播放它们。

什么格式最适合此类样本?我正在寻找良好的压缩、良好的质量和方便的 Java 编程使用的结合体。

I'm developing a Java game and want to bundle and play a number of sampled sound effects. I'm planning to use the standard javax.sound.sampled functionality to play them.

What format is best for these kind of samples? I'm looking for a blend of good compression, decent quality and convenience for programmatic use within Java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绅士风度i 2024-09-15 22:30:52

质量与大小

这里有一些关于 MP3 与 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。请参阅此处的文档。

Quality vs. Size

Here are some interesting articles on the differences of MP3 vs. WAV.

Why MP3 and WAV? These are the ones I see most often when creating program, so compatibility is never an issue.

I have this really useful java file that does everything for you. You construct the object with the path to the sound file, then use methods to play, stop, loop it, etc.

In the meantime, you can look at these for reference, albeit they're not as clean: How can I play sound in Java? .


Simple Programming

Unfortunately, I don't have the file on this computer, but here's a simplified version. This doesn't use javax.swing like you'd hoped, but to be honest, I've always preferred alternative methods.

Because this is not the same as the file I have on my other computer, I cannot guarantee MP3 compatibility.

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

}

Instantiation:

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 the clip to be played.

I prefer this method so you don't have to constantly set everything up a stream each time you want to play a clip. This should work effectively with a few sound bites and blurbs, but be sure not to overuse it and take up memory. Use this for common sound effects.


Simple Programming, Cont'd

Found a good file to play MP3s in the same way as I showed above. I completely forgot to put mine in a separate thread, so this is a much better bet.

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

    }

}

Easy as pie, right? ;).

Get the jar here. See the documentation here.

书信已泛黄 2024-09-15 22:30:52

Java 支持 mp3,而 mp3 格式使您可以选择在压缩与质量之间进行平衡。

有一个纯java MP3解码器,http://www.javazoom.net/javalayer/javalayer。 html,它插入 javax.sound 包中。 Sun 还发布了 mp3 插件 java的声音。

Java has support for mp3, and the mp3 format gives you the choice where to balance compression vs. quality.

There is a pure java MP3 decoder, http://www.javazoom.net/javalayer/javalayer.html, which plugs into the javax.sound package. Sun also have released a mp3 plugin for java sound.

请恋爱 2024-09-15 22:30:52

我不是这方面的专家,但 Sun 的所有教程和示例都适用于 .au 文件。似乎是有保证的最低公分母。为了使文件格式更加灵活,您需要使用类似 的内容Java 媒体框架

I'm no expert on this, but all the tutorials and examples from Sun work with .au files. Seems to be a guaranteed lowest-common-denominator. For more flexibility in file formats, you'd need to work with something like the Java Media Framework.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文