当前位置: 代码迷 >> J2SE >> Java打JAR时遇到的传路径有关问题
  详细解决方案

Java打JAR时遇到的传路径有关问题

热度:8800   发布时间:2013-02-25 00:00:00.0
Java打JAR时遇到的传路径问题
public class Mp3Player {
private File fis;
private boolean flag;
private Thread thread;

public Mp3Player(File fis,boolean flag){
System.out.println(fis);
this.fis=fis;
this.flag=flag;
new Player();
}
public Thread getThread(){
return thread;
}
public class Player implements Runnable{
public Player(){
thread =new Thread(this);
thread.start();
}
public void run(){
while(true){
try{
//File file =new File(fis.getFile());
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(fis);
AudioFormat audioFormat = audioInputStream.getFormat();
// 转换mp3文件编码
 
if (audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {

audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,

audioFormat.getSampleRate(), 16, audioFormat

.getChannels(), audioFormat.getChannels() * 2,

audioFormat.getSampleRate(), false);

audioInputStream = AudioSystem.getAudioInputStream(audioFormat,

audioInputStream);

}
// 打开输出设备

DataLine.Info dataLineInfo = new DataLine.Info(

SourceDataLine.class, audioFormat,

AudioSystem.NOT_SPECIFIED);

SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);

sourceDataLine.open(audioFormat);

sourceDataLine.start();
   
int cnt;
byte tempBuffer[] = new byte[320];
while ((cnt = audioInputStream.read(tempBuffer, 0,
tempBuffer.length)) != -1) {
if (cnt > 0) {
// 写入缓存数据
sourceDataLine.write(tempBuffer, 0, cnt);
}

}
// Block等待临时数据被输出为空
sourceDataLine.drain();
sourceDataLine.close();
if(!flag) break;
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
这个是我播放音乐的类.
if(e.getSource()==onMusic&&musicFlag){
//String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath()+"/com/mouse/sounds/background.mp3";
File file = null;
try {
file = new File(getClass().getResource("sounds/background.mp3").toURI());
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//File file=new File(path);
mp3=new Mp3Player(file,true);

// getClass().getResource("sounds/background.mp3").;
musicFlag=false;
}
在MYECLIPSE里运行没错误,但是一打JAR包音乐就会出错,求高人帮助呀

------解决方案--------------------------------------------------------
路径的写法好像没错吧,是不是你打完jar包后,确保sounds的文件夹和jre的文件夹 都拷贝出来了,sounds不要放在src的文件夹下(我说的是放在一个没有jdk环境的电脑上的),可以参考一下
------解决方案--------------------------------------------------------
手动打jar包比较保险一些 用程序打包一般都会出错的
------解决方案--------------------------------------------------------
要连同第三方jar一起打入jar包的
------解决方案--------------------------------------------------------
Java code
import java.io.IOException;import java.io.InputStream;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;import javax.sound.sampled.UnsupportedAudioFileException;public class Mp3Player {    private InputStream fis;    private boolean flag;    private Thread thread;    public Mp3Player(InputStream fis, boolean flag) {        System.out.println(fis);        this.fis = fis;        this.flag = flag;        new Player();    }    public Thread getThread() {        return thread;    }    public class Player implements Runnable {        public Player() {            thread = new Thread(this);            thread.start();        }        public void run() {            while (true) {                try {                    // File file =new File(fis.getFile());                    AudioInputStream audioInputStream = AudioSystem                            .getAudioInputStream(fis);                    AudioFormat audioFormat = audioInputStream.getFormat();                    // 转换mp3文件编码                    if (audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {                        audioFormat = new AudioFormat(                                AudioFormat.Encoding.PCM_SIGNED,                                audioFormat.getSampleRate(), 16, audioFormat                                .getChannels(), audioFormat.getChannels() * 2,                                audioFormat.getSampleRate(), false);                        audioInputStream = AudioSystem.getAudioInputStream(                                audioFormat,                                audioInputStream);                    }                    // 打开输出设备                    DataLine.Info dataLineInfo = new DataLine.Info(                    SourceDataLine.class, audioFormat,                    AudioSystem.NOT_SPECIFIED);                    SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem                            .getLine(dataLineInfo);                    sourceDataLine.open(audioFormat);                    sourceDataLine.start();                    int cnt;                    byte tempBuffer[] = new byte[320];                    while ((cnt = audioInputStream.read(tempBuffer, 0,                            tempBuffer.length)) != -1) {                        if (cnt > 0) {                            // 写入缓存数据                            sourceDataLine.write(tempBuffer, 0, cnt);                        }                    }                    // Block等待临时数据被输出为空                    sourceDataLine.drain();                    sourceDataLine.close();                    if (!flag)                        break;                } catch (Exception e) {                    e.printStackTrace();                    break;                }            }        }    }    public static void main(String[] args) throws UnsupportedAudioFileException, IOException{        new Mp3Player(Mp3Player.class.getResourceAsStream("/...superpqckages/sounds/pcm-8000-16-2.wav"), true);    }}
  相关解决方案