gpt4 book ai didi

Java - 在 JButton 悬停/单击/释放时播放声音

转载 作者:行者123 更新时间:2023-12-02 06:08:51 29 4
gpt4 key购买 nike

我一直在使用 Technic 的源代码为 Minecraft 编写一个启动器,并且我已经成功地制作了一个看起来非常漂亮的分支。

为了使启动器更加独特,除了改变外观之外,我决定添加一个声音 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)

最佳答案

soundHover 指向什么?它说声音为空,所以你确定你正确调用了文件吗?

关于Java - 在 JButton 悬停/单击/释放时播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22052499/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com