1

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();
        }
    });
CSE
  • 73
  • 6
  • What are the arguments for the MP3Player initialiser/Constructor? Does it take File type or URL? – apollosoftware.org Apr 26 '22 at 18:49
  • @apollosoftware.org this time `Cardinal System` who give the answer can able to play music. But this time only one specific song is being played. Of the many songs on JList, the one that I click on is only playing a specific song (YaRabba.mp3). – CSE Apr 26 '22 at 18:54

2 Answers2

2

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.

Cardinal System
  • 2,749
  • 3
  • 21
  • 42
  • He's right, Assuming that the constructor for MP3Player takes File type as the argument. – apollosoftware.org Apr 26 '22 at 18:50
  • @Cardinal System It's working. But this time only one specific song is being played. Of the many songs on JList, the one that I click on is only playing a specific song (YaRabba.mp3). – CSE Apr 26 '22 at 18:51
  • You're going to have to write LOTS more code to check to see if it's finished. It will be a rudimentary state machine. Assuming that the MP3Player class is just a raw player, it probably doesn't have queuing or player states. There's plenty of code out there, so I wouldn't write from scratch. – apollosoftware.org Apr 26 '22 at 19:03
  • @apollosoftware.org What can i do now? – CSE Apr 26 '22 at 19:11
1

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.

apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69
  • Your Solution is also working . But the problem is as same as i mentioned in comments. this time only one specific song is being played. Of the many songs on JList, the one that I click on is only playing a specific song (YaRabba.mp3). – CSE Apr 26 '22 at 19:01
  • The way the code is working is that file : mp3Files will go through every single file in that directory, and play it, ending with the last one. What you need to do is take mp3 = new MP3Player(file) out of that loop! – apollosoftware.org Apr 26 '22 at 19:10
  • Since it's in the for loop, it'll try and play every single mp3 in that directory one after another. So your code right now is basically, playing every single song (only for a millisecond), and then the very last one YaRabba is playing at the end :) – apollosoftware.org Apr 26 '22 at 19:12
  • I take it out the loop. but `file` is showing error. because it needs to be created local variable. Can you please give me a solution with the out of loop `mp3 = new MP3Player(file)` this line? because the `file` shows error. – CSE Apr 26 '22 at 19:19
  • 1
    Create an array of Files[] and then you can play the first element of Files[0]. You will have to write code to play File[0] to File[file count-1] outside of the loop. – apollosoftware.org Apr 26 '22 at 19:22
  • 1
    It's a collection so you'll need to do https://stackoverflow.com/questions/1671378/java-get-first-item-from-a-collection – apollosoftware.org Apr 26 '22 at 19:28
  • I am trying your solutions :) – CSE Apr 26 '22 at 19:33
  • 1
    mp3 = new MP3Player(Iterables.get(mp3Files, 0)); or something similar – apollosoftware.org Apr 26 '22 at 19:35