我想从 jar 文件本身加载一个 mp3 文件,然后用 JLayer 播放它。当我尝试这样做时,我会得到 NullPointerException 或 FileNotFoundException。关于如何解决这个问题的任何想法?
//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) {
}
}