【发布时间】:2014-06-28 17:26:47
【问题描述】:
打包soundcliptest;
// development environment(NetBeans 8.0)
//
// NetBeansProjects
// SoundClipTest
// Source Packages
// resources
// ding.wav
// soundcliptest
// SoundClipTest.java
//
// unZipped jar file
//
// SoundClipTest
// META-INF
// resourses
// ding.wav
// soudcliptest
// SoundClipTest.class
//
伙计们,我还在学习如何使用这个工具。我似乎无法获得它们所属的进口。
我需要知道如何从代码中查看 jar 文件。 File 方法无法破解它。必须有某种方法可以找到资源“目录”的内容。我想要做的是在 /resources/ 下制作声音文件的菜单。我可以使它在开发环境中工作,但不能从 jar 文件中工作。也许一些'zip'方法?但我还没有找到它们。提前致谢。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
public class SoundClipTest extends JFrame {
JTextPane myPane;
String title;
String showIt;
public SoundClipTest() {
// get something to write on
this.myPane = new JTextPane();
this.myPane.setPreferredSize(new Dimension(700, 100));
this.myPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
try {
// Open an audio input stream.
URL dingUrl = getClass().getResource("/resources/ding.wav");
// show the path we got
String path = dingUrl.getPath();
//trim 'ding.wav' from file path to get a directory path
String dirPath = path.substring(0, path.length()-"ding.wav".length());
// now get a Url for the dir from getResource and show THAT path
URL dirUrl = getClass().getResource("/resources/");
String urlPath = dirUrl.getPath();
// the dirUrl path is just like the trimmed 'ding' file path
// so use urlPath to get a file object for the directory
try {
File f = new File(urlPath); //works fine in dev environment
String filePath = f.getPath(); // but not from jar file
title = f.list()[0]; // from jar, null pointer exception here
// whan things go right (HA HA) we display this
showIt = (" >>>>> IT WORKED!! <<<<<!"
+ "\n path to file: "+ path
+ "\n path to dir: " + dirPath
+ "\n path from URL: " + urlPath
+ "\n path from file: "+ filePath + "\n " + title);
} catch (Exception e) {
// you get this on display when executing the jar file
showIt = (" PHOOEY"
+ "\n the ding " + path
+ "\n trimmed path " + dirPath
+ "\n the URL path " + urlPath
+ "\n could not create a File object from path");
// the stack trace shows up only if you run from the terminal
e.printStackTrace();
}
// We get a nice little 'ding', anyway
AudioInputStream ais = AudioSystem.getAudioInputStream(dingUrl);
Clip clip = AudioSystem.getClip();
clip.open(ais);
clip.start();
//but nuttin else good - show the disapointing results
myPane.setText(showIt);
} catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
System.out.println("Ouch!!! Damn, that hurt!");
e.printStackTrace();
}
}
public static void main(String[] args) {
SoundClipTest me = new SoundClipTest();
JFrame frame = new JFrame("Sound Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(me.myPane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
【问题讨论】:
-
嗯...有没有什么地方可以找到关于如何使用这个工具的解释?还是我只是继续试验。如果必须的话,我想我可以自己解决。