在Java中播放.mp3文件时遇到了麻烦
我想做一个简单的音乐播放器。因此,我从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改此行:
对此:
<代码>文件#getName 返回文件的名称,而不是绝对路径。另外,当您已经拥有
文件
变量时,无需构建一个新的文件实例。Change this line:
to this:
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 yourfile
variable.请记住要注意您的格式。通常,使用前斜线保持路径是一个很好的做法。 C:/Music
确保您的路径不是我尝试将某些MP3文件加载到某个根目录或相同路径的问题(例如C:/Music,甚至与源的目录相同的目录。/)。然后使用上述更改并测试:
如果有效,则可以尝试使用不同的路径。如果出错,那么您会知道您的路径搞砸了。
** 修改的。您的代码正在播放该循环中的每首歌曲。我随时写了这篇文章,但是您要做的就是使用您的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:
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.