【发布时间】:2014-02-26 20:25:56
【问题描述】:
我一直在使用 Technic 的源代码为 Minecraft 编写一个 Launcher,并且我已经成功地制作了一个非常漂亮的分支。
为了使启动器更加独特,除了改变外观之外,我决定添加一个声音 API,允许我在悬停/单击/释放按钮时播放 *.wav、*.ogg 文件在启动器中。我使用的是我自己的组件,但它们扩展了 Swing 组件,因此我可以在我的代码中访问 mouseClicked、mousePressed、mouseReleased、mouseEntered、mouseExited 事件。
我查看了几个向 Java 应用程序添加声音的不同示例,并尝试对其进行调整以适合我的应用程序。但没有运气。
这是我的 AudioAPI 类的来源:
package uk.co.roguerage.launcher.main.ui.components.api;
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class AudioAPI {
private AudioFormat audioFormat;
private AudioInputStream audioInputStream;
private SourceDataLine sourceDataLine;
public void playAudio(File soundFile) {
try {
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
audioFormat = audioInputStream.getFormat();
System.out.println(audioFormat);
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo);
new PlayThread().start();
} catch( Exception e ) {
e.printStackTrace();
}
}
class PlayThread extends Thread {
byte tempBuffer[] = new byte[10000];
public void run() {
try {
sourceDataLine.open(audioFormat);
sourceDataLine.start();
int cnt;
while( (cnt = audioInputStream.read(tempBuffer,0,tempBuffer.length)) != -1 ) {
if( cnt > 0 ) {
sourceDataLine.write(tempBuffer, 0, cnt);
}
}
sourceDataLine.drain();
sourceDataLine.close();
} catch( Exception e ) {
e.printStackTrace();
}
}
}
}
我使用以下代码调用 API:
AudioAPI audioAPI = new AudioAPI();
audioAPI.playAudio(soundHover);
每当我将鼠标悬停在按钮上时,我似乎得到的只是一个错误。这是错误:
[B#1] java.lang.NullPointerException
[B#1] at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
[B#1] at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
[B#1] at uk.co.roguerage.launcher.main.ui.components.api.AudioAPI.playAudio(AudioAPI.java:18)
[B#1] at uk.co.roguerage.launcher.main.ui.components.LiteButton.mouseEntered(LiteButton.java:88)
[B#1] at java.awt.AWTEventMulticaster.mouseEntered(Unknown Source)
[B#1] at java.awt.Component.processMouseEvent(Unknown Source)
[B#1] at javax.swing.JComponent.processMouseEvent(Unknown Source)
[B#1] at java.awt.Component.processEvent(Unknown Source)
[B#1] at java.awt.Container.processEvent(Unknown Source)
[B#1] at java.awt.Component.dispatchEventImpl(Unknown Source)
[B#1] at java.awt.Container.dispatchEventImpl(Unknown Source)
[B#1] at java.awt.Component.dispatchEvent(Unknown Source)
[B#1] at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
[B#1] at java.awt.LightweightDispatcher.trackMouseEnterExit(Unknown Source)
[B#1] at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
[B#1] at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
[B#1] at java.awt.Container.dispatchEventImpl(Unknown Source)
[B#1] at java.awt.Window.dispatchEventImpl(Unknown Source)
[B#1] at java.awt.Component.dispatchEvent(Unknown Source)
[B#1] at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
[B#1] at java.awt.EventQueue.access$000(Unknown Source)
[B#1] at java.awt.EventQueue$3.run(Unknown Source)
[B#1] at java.awt.EventQueue$3.run(Unknown Source)
[B#1] at java.security.AccessController.doPrivileged(Native Method)
[B#1] at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
[B#1] at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
[B#1] at java.awt.EventQueue$4.run(Unknown Source)
[B#1] at java.awt.EventQueue$4.run(Unknown Source)
[B#1] at java.security.AccessController.doPrivileged(Native Method)
[B#1] at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
[B#1] at java.awt.EventQueue.dispatchEvent(Unknown Source)
[B#1] at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
[B#1] at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
[B#1] at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
[B#1] at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
[B#1] at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
[B#1] at java.awt.EventDispatchThread.run(Unknown Source)
【问题讨论】: