【LWJGL2 WIKI】【輔助庫篇】Slick-Util庫:第二部分-讀取聲音

原文:http://wiki.lwjgl.org/wiki/Slick-Util_Library_-Part_2-_Loading_Sounds_for_LWJGL

Java自帶支持WAV和AIF格式,Slick-Util自然也可以。另外Slice-Util通過使用外部庫還能夠支持OGG和XM格式。聲音格式既可以讀進內存中,也可以從文件流讀取。

OGG Support OGG支持

OGG是一種可以與MP3相媲美的專利免費格式。Slick-Util通過使用了Vorbis庫支持了它。因此如果你想讀OGG文件,你需要加入jorbis-.jar和jogg-.jar文件到classpath中。這些可以在Slick-Util的壓縮包裏找到。

XM Format XM格式

XM格式是一種聲軌格式,由IBXM庫支持。因此需要加入ibxm.jar文件到classpath,同樣在壓縮包裏能找到。

Basics 基礎

用Slick-Util讀取聲音很簡單,跟讀圖片相似。所有聲文件存在Audio類中,用AudioLoader類讀取。
既然你用OpenAL,別忘了在程序關閉前調用AL.destroy()方法來釋放資源。

import java.io.IOException;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.openal.AL;
import org.newdawn.slick.openal.Audio;
import org.newdawn.slick.openal.AudioLoader;
import org.newdawn.slick.openal.SoundStore;
import org.newdawn.slick.util.ResourceLoader;

public class SoundExample {
    /** The ogg sound effect */
    private Audio oggEffect;
    /** The wav sound effect */
    private Audio wavEffect;
    /** The aif source effect */
    private Audio aifEffect;
    /** The ogg stream thats been loaded */
    private Audio oggStream;
    /** The mod stream thats been loaded */
    private Audio modStream;

    /**
     * Start the test
     */
    public void start() {
        initGL(800,600);
        init();

        while (true) {
            update();
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
            render();

            Display.update();
            Display.sync(100);

            if (Display.isCloseRequested()) {
                Display.destroy();
                AL.destroy();
                System.exit(0);
            }
        }
    }

    /**
     * Initialise the GL display
     *
     * @param width The width of the display
     * @param height The height of the display
     */
    private void initGL(int width, int height) {
        try {
            Display.setDisplayMode(new DisplayMode(width,height));
            Display.create();
            Display.setVSyncEnabled(true);
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }

        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glShadeModel(GL11.GL_SMOOTH);       
        GL11.glDisable(GL11.GL_DEPTH_TEST);
        GL11.glDisable(GL11.GL_LIGHTING);                   

        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);               
        GL11.glClearDepth(1);                                      

        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

        GL11.glViewport(0,0,width,height);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, width, height, 0, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
    }

    /**
    * Initialise resources
    */
    public void init() {

        try {
        // you can play oggs by loading the complete thing into
        // a sound
        oggEffect = AudioLoader.getAudio("OGG", ResourceLoader.getResourceAsStream("testdata/restart.ogg"));

        // or setting up a stream to read from. Note that the argument becomes
        // a URL here so it can be reopened when the stream is complete. Probably
        // should have reset the stream by thats not how the original stuff worked
        oggStream = AudioLoader.getStreamingAudio("OGG", ResourceLoader.getResource("testdata/bongos.ogg"));

        // can load mods (XM, MOD) using ibxm which is then played through OpenAL. MODs
        // are always streamed based on the way IBXM works
        modStream = AudioLoader.getStreamingAudio("MOD", ResourceLoader.getResource("testdata/SMB-X.XM"));

        // playing as music uses that reserved source to play the sound. The first
        // two arguments are pitch and gain, the boolean is whether to loop the content
        modStream.playAsMusic(1.0f, 1.0f, true);

        // you can play aifs by loading the complete thing into
        // a sound
        aifEffect = AudioLoader.getAudio("AIF", ResourceLoader.getResourceAsStream("testdata/burp.aif"));

        // you can play wavs by loading the complete thing into
        // a sound
        wavEffect = AudioLoader.getAudio("WAV", ResourceLoader.getResourceAsStream("testdata/cbrown01.wav"));
        } catch (IOException e) {
        e.printStackTrace();
    }
    }

    /**
     * Game loop update
     */
    public void update() {
        while (Keyboard.next()) {
            if (Keyboard.getEventKeyState()) {
                if (Keyboard.getEventKey() == Keyboard.KEY_Q) {
                    // play as a one off sound effect
                    oggEffect.playAsSoundEffect(1.0f, 1.0f, false);
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_W) {
                    // replace the music thats curretly playing with
                    // the ogg
                    oggStream.playAsMusic(1.0f, 1.0f, true);
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_E) {
                    // replace the music thats curretly playing with
                    // the mod
                    modStream.playAsMusic(1.0f, 1.0f, true);
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_R) {
                    // play as a one off sound effect
                    aifEffect.playAsSoundEffect(1.0f, 1.0f, false);
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_T) {
                    // play as a one off sound effect
                    wavEffect.playAsSoundEffect(1.0f, 1.0f, false);
                }
            }
        }

        // polling is required to allow streaming to get a chance to
        // queue buffers.
        SoundStore.get().poll(0);
    }

    /**
     * Game loop render
     */
    public void render() {

    }

    /**
     * Main method
     */
    public static void main(String[] argv) {
        SoundExample soundExample = new SoundExample();
        soundExample.start();
    }
}

Credit

Tutorial Credit - Ninja Cave

Kevin Glass for writing the Slick Library and initial example code.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章