1
我想從jar文件本身加載一個mp3文件,然後用JLayer播放。當我嘗試這樣做時,我得到一個NullPointerException或一個FileNotFoundException。任何想法如何解決這個問題?從Jar中加載JLayer的mp3文件
//MP3Player.java
package Music;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javazoom.jl.player.Player;
public class MP3Player extends Thread
{
private String filename;
private Player player;
private Thread mp3Thread;
private boolean playing;
private boolean locked;
// constructor that takes the name of an MP3 file
public MP3Player()
{
this.filename = "";
mp3Thread = null;
playing = true;
locked = true;
}
public void run()
{
while (playing)
play();
}
public void play()
{
if (!locked)
{
if (filename != null && filename != "")
{
try
{
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new Player(bis);
mp3Thread = new Thread()
{
public void run()
{ try { player.play(); } catch (Exception e) { System.out.println(e); } }
};
mp3Thread.start();
// mp3Thread.join();
} catch (Exception e) { System.out.println(e); }
}
}
}
public void changeMusic(String newGuy)
{
locked = true;
filename = "";
try
{
if (player != null)
player.close();
mp3Thread = null;
filename = newGuy;
locked = false;
}
catch (Exception e) {}
}
public void close()
{
locked = true;
playing = false;
try
{
if (player != null)
player.close();
if (mp3Thread != null)
mp3Thread = null;
} catch (Exception e) {}
}
// MusicHandler.java
package Music;
import java.io.File;
import javax.swing.*;
public final class MusicHandler {
public MusicHandler() {
}
public void PlayBoundlessMusic() {
try {
String myFile = (getClass().getResource("/Boundless/Twilight.mp3").getPath());
MP3Player player = new MP3Player();
player.changeMusic(myFile);
player.play();
} catch (Exception e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, e);
}
}
private void PlayMusic(File f) {
}
}