1

I want to get path of a music file and than add that file in my MP3-player using this code bellow:

addSong.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (fileChooser.showSaveDialog(MainFrame.this) == JFileChooser.APPROVE_OPTION) {
                File song = fileChooser.getSelectedFile();

                mainPanel.addSongs(song.getAbsolutePath()); // getting file path

                JOptionPane.showMessageDialog(MainFrame.this,
                        "Song has been successfully added to your music track!", "Information",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        }
    });

Than I pass it like this:

public void addSongs(String path) {
    musicPlayer.addSongs(path);
}

And in the end it comes here:

public void addSongs(String path) {

    Media media = new Media(path); // this is place where error occurs

    MediaPlayer mediaPlayer = new MediaPlayer(media);

    songList.add(mediaPlayer);

}

And I got an error:

Exception in thread "AWT-EventQueue-0"        java.lang.IllegalArgumentException: java.net.URISyntaxException: Illegal character in opaque part at index 2: C:\Users\Jovan\Desktop\DEAF KEV - Invincible [NCS Release].mp3
at javafx.scene.media.Media.<init>(Media.java:385)
at mp3.MusicPlayer.addSongs(MusicPlayer.java:86)
at mp3.MainPanel.addSongs(MainPanel.java:40)
at mp3.MainFrame$2.actionPerformed(MainFrame.java:78)

How to solve this problem ?

  • Apparently, `C:\Users\Jovan\Desktop\DEAF KEV - Invincible [NCS Release].mp3` is not a valid `URI` – xingbin Mar 08 '18 at 14:48
  • Possible duplicate of [Android: howto parse URL String with spaces to URI object?](https://stackoverflow.com/questions/2593214/android-howto-parse-url-string-with-spaces-to-uri-object) – Lance Toth Mar 08 '18 at 14:50
  • Try with this instead: `file:///C:/Users/Jovan/Desktop/DEAF%20KEV%20-%20Invincible%20%5BNCS%20Release%5D.mp3` – Patrick Mevzek Mar 08 '18 at 14:59
  • It doesn't work. I have also tried this : Android: how to parse URL String with spaces to URI object? but it doesn't work – Jovan Dukic Mar 08 '18 at 15:04

0 Answers0