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包音乐就会出错,求高人帮助呀

解决方案 »

  1.   

    路径的写法好像没错吧,是不是你打完jar包后,确保sounds的文件夹和jre的文件夹 都拷贝出来了,sounds不要放在src的文件夹下(我说的是放在一个没有jdk环境的电脑上的),可以参考一下
      

  2.   


    jre文件夹?
    我把引用的JAR包都打到lib里了呀.. 在打出JAR包里也有...我真心不知道什么情况...
      

  3.   

    我真心郁闷了,写个JAVA小项目,打JAR就运行不了,这也太打击我学JAVA的信心了,什么呀,相对路径这么恶心,这是什么功能强大的语言...
      

  4.   

    手动打jar包比较保险一些  用程序打包一般都会出错的
      

  5.   

    要连同第三方jar一起打入jar包的
      

  6.   

    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);
    }
    }我这样试了试,打jar也没问题啊
      

  7.   

    Sorry,是我犯傻了,打JAR的时候没有把用过的第三方JAR引用到...   现在问题解决了,谢谢大家