简体   繁体   English

无法播放 Java 中的 .mp3 文件

[英]Having trouble playing .mp3 files in Java

I want to make a simple music player.我想做一个简单的音乐播放器。 So I filtered the .mp3 files from the PC directory and showed them in JList .所以我从 PC 目录中过滤了.mp3文件并将它们显示在JList中。 But when I select the song from JList , the song is not playing and showing error like this但是当我 select 来自JList的歌曲时,这首歌没有播放并显示这样的error

java.io.FileNotFoundException: F:\Java in Eclipse\NewMusicPlayer\(song name).mp3 (The system cannot find the file specified)

My Code is given below我的代码如下

Code of File Filtering:文件过滤代码:

MP3Player mp3;

private void setMusic() {
    File home = new File("E:\\MUSICS COLLECTION");
    Collection<File> mp3Files = FileUtils.listFiles(home, new String[] { "mp3" }, true);
    @SuppressWarnings("rawtypes")
    DefaultListModel showData = new DefaultListModel();
    for (File file : mp3Files) {
        showData.addElement(file.getName());
        mp3 = new MP3Player(new File(file.getName()));
    }
    musicList.setModel(showData);

}

Code of JList Selected Item: JList选中项的代码:

musicList = new JList();
    musicList.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent arg0) {
            mp3.play();
        }
    });

Change this line:更改此行:

mp3 = new MP3Player(new File(file.getName()));

to this:对此:

mp3 = new MP3Player(file);

File#getName returns the name of the file, not the absolute path. File#getName返回文件的名称,而不是绝对路径。 Also, there's no need to construct a new file instance when you already have your file variable.此外,当您已经拥有file变量时,无需构建新的文件实例。

REMEMBER to mind your formatting.请记住您的格式。 It's generally good practice to keep your paths using forward slash.使用正斜杠保留路径通常是一种很好的做法。 C:/Music C:/音乐

To make sure your path is not the issue I'd try and load some mp3 files into some root directory or the same path (for example C:/Music, or even the same directory as the source./).为了确保您的路径不是问题,我会尝试将一些 mp3 文件加载到某个根目录或相同的路径(例如 C:/Music,甚至与源相同的目录./)。 Then use the above-mentioned changes and test:然后使用上述更改并测试:

    File home = new File("./");
    Collection<File> mp3Files = FileUtils.listFiles(home, new String[] { "mp3" }, true);
    @SuppressWarnings("rawtypes")
    DefaultListModel showData = new DefaultListModel();

    for (File file : mp3Files) {
        showData.addElement(file.getName());
    }
    // the firstFile is just a example. You should store it into an array of Files[]. 
    // or better yet since it's already a Collection use Iterables and get firstElement.
    mp3 = new MP3Player(Iterables.get(mp3Files, 0));
    musicList.setModel(showData);

If that works then you can try with different paths.如果可行,那么您可以尝试使用不同的路径。 If it errors, then you'll know your path is messed up.如果它出错,那么你就会知道你的路径搞砸了。

** Modified. ** 修改的。 Your code is playing every single song in that For Loop.您的代码正在播放该 For 循环中的每一首歌曲。 I wrote this on the fly, but what you'll have to do is use your mp3Files collection, and use the Iterables class to grab whatever index element you want.我是即时写的,但你必须做的是使用你的 mp3Files 集合,并使用 Iterables class 来获取你想要的任何索引元素。 For example above written grabs 0 index.例如上面写的抓取 0 索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM