我试图使用JLayer库在Java中创建一个基本的音乐播放器,但是它似乎不起作用。通过JFileChooser打开文件(.mp3)时没有任何反应。下面是我的应用程序的代码。请告诉我其中有什么问题。
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.swing.JFileChooser;
import javazoom.jl.player.Player;
import javazoom.jl.decoder.JavaLayerException;
class MusicPlayer
{
public void Player()
{
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
try
{
File track = chooser.getSelectedFile();
FileInputStream trackstream = new FileInputStream(track);
BufferedInputStream bufferedtrack = new BufferedInputStream(trackstream);
try
{
Player player = new Player(bufferedtrack);
}
catch(JavaLayerException e)
{
System.out.println("Can't open file!");
}
}
catch(FileNotFoundException e)
{
}
}
else {
}
}
}
public class PlayerApp {
public static void main(String[] args) {
new MusicPlayer().Player();
}
}
答案 0 :(得分:0)
您忘记在player.play();
之后调用Player player = new Player(bufferedtrack);
方法
答案 1 :(得分:0)
您确实错过了Player中的player.play()
来开始播放所请求的文件。
try
{
Player player = new Player(bufferedtrack);
player.play(); // add this line
}
catch(JavaLayerException e)
{
System.out.println("Can't open file!");
}
此外,方法名称通常用camel-case书写(首字母小写)。我建议将您的方法void Player()
重命名为void loadDialogAndPlayFile()
之类。