我想使用java代码播放.wav文件,该代码在jar文件中作为资源。我的代码看起来像这样 -
try {
URL defaultSound = getClass().getResource("/images/ads/WindowsNavigationStart.wav");
// getClass().getSy.getResource("/images/ads/WindowsNavigationStart.wav");
File soundFile = new File(defaultSound.toURI());
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start( );
} catch (Exception ex) {
ex.printStackTrace();
}
文件WindowsNavigationStart.wav存在于我的一个jar文件中。但得到以下异常 -
java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.<init>(File.java:363)
at au.com.webscan.wwwizard.app.admin.customfile.UpOneLevelFolder.btnUpFolderActionPerformed(Unknown Source)
at au.com.webscan.wwwizard.app.admin.customfile.UpOneLevelFolder.access$000(Unknown Source)
at au.com.webscan.wwwizard.app.admin.customfile.UpOneLevelFolder$1.actionPerformed(Unknown Source)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
at java.awt.Component.processMouseEvent(Component.java:6288)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6053)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4651)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
请给我一个解决方案。谢谢大家。
答案 0 :(得分:7)
你试过了吗?
InputStream is= getClass().getResourceAsStream("/images/ads/WindowsNavigationStart.wav");
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(is);
基本上我认为你不能在jar文件中用URI创建一个文件。但是您可以直接传递输入流。
答案 1 :(得分:5)
变化:
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile);
要:
System.out.println("defaultSound " + defaultSound); // check the URL!
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(defaultSound);
答案 2 :(得分:3)
获得inputStream的句柄后,获取audioInputStream并完成剩下的工作。
InputStream is = getClass().getResourceAsStream("......");
AudioInputStream ais = AudioSystem.getAudioInputStream(is);
Clip clip = AudioSystem.getClip();
clip.open(ais);
答案 3 :(得分:2)
完美解决方案.......
URL url = this.getClass().getResource("sounds/beep.au");
String urls=url.toString();
urls=urls.replaceFirst("file:/", "file:///");
AudioClip ac=Applet.newAudioClip(new URL(urls));
ac.play();
答案 4 :(得分:1)
请参阅我之前在making a single-jar java application的回答。标题是误导性的,但海报试图做一些几乎与你相同的事情。一些最佳细节位于聊天记录的链接中。
答案 5 :(得分:0)
try {
AudioPlayer.player.start(new AudioStream(getClass().getResourceAsStream("/sound/SystemNotification.wav")));
} catch (Exception e) {
e.printStackTrace();
}
答案 6 :(得分:0)
这对我来说很好:
public void playSound() {
InputStream in;
try {
in = new BufferedInputStream(new FileInputStream(new File(
getClass().getClassLoader()
.getResource("com/kaito/resources/sound.wav").getPath())));
AudioStream audioStream = new AudioStream(in);
AudioPlayer.player.start(audioStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
答案 7 :(得分:0)
以下内容允许我在Eclipse项目和导出的jar文件中播放声音:
- 注意使用BufferedInputStream
- 注意,使用inputStream而不是file。
在我的主要():
playAlarmSound();
在我班上:
public static void playAlarmSound() {
ClassLoader classLoader = App.class.getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("alarmsound.wav");
try {
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(inputStream));
clip.open(ais);
clip.start();
} catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) {
System.err.println("ERROR: Playing sound has failed");
e.printStackTrace();
}
}