【LWJGL2 WIKI】【辅助库篇】Slick-Util库:第二部分-读取声音
2016-01-03 16:55
411 查看
原文:http://wiki.lwjgl.org/wiki/Slick-Util_Library_-Part_2-_Loading_Sounds_for_LWJGL
Java自带支持WAV和AIF格式,Slick-Util自然也可以。另外Slice-Util通过使用外部库还能够支持OGG和XM格式。声音格式既可以读进内存中,也可以从文件流读取。
既然你用OpenAL,别忘了在程序关闭前调用AL.destroy()方法来释放资源。
Kevin Glass for writing the Slick Library and initial example code.
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 CaveKevin Glass for writing the Slick Library and initial example code.
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树