在Java中播放.mp3文件时遇到了麻烦

发布于 2025-01-24 04:33:45 字数 1084 浏览 1 评论 0原文

我想做一个简单的音乐播放器。因此,我从PC目录中过滤了.mp3文件,并在jlist中显示了它们。但是,当我从jlist中选择歌曲时,歌曲没有播放,并且显示错误像这样的

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

我的代码在下面给出

文件过滤代码:

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);

}

jlist选定项目的代码:

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

I want to make a simple music player. So I filtered the .mp3 files from the PC directory and showed them in JList. But when I select the song from JList, the song is not playing and showing error like this

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 :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

べ映画 2025-01-31 04:33:45

更改此行:

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

对此:

mp3 = new MP3Player(file);

<代码>文件#getName 返回文件的名称,而不是绝对路径。另外,当您已经拥有文件变量时,无需构建一个新的文件实例。

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. Also, there's no need to construct a new file instance when you already have your file variable.

旧人 2025-01-31 04:33:45

请记住要注意您的格式。通常,使用前斜线保持路径是一个很好的做法。 C:/Music

确保您的路径不是我尝试将某些MP3文件加载到某个根目录或相同路径的问题(例如C:/Music,甚至与源的目录相同的目录。/)。然后使用上述更改并测试:

    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);

如果有效,则可以尝试使用不同的路径。如果出错,那么您会知道您的路径搞砸了。

** 修改的。您的代码正在播放该循环中的每首歌曲。我随时写了这篇文章,但是您要做的就是使用您的MP3Files Collection,然后使用Iterables类来获取所需的任何索引元素。例如,上面的书面抓取0索引。

REMEMBER to mind your formatting. It's generally good practice to keep your paths using forward slash. C:/Music

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 ./). 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. 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. For example above written grabs 0 index.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文