0

Hi I have created a file chooser and I am wondering if there is any possible way to play an mp3 file that I can select from my file chooser. If so how can that be implemented ? Thanks for the advice in advance.So Apparently when I click to a file nothing happens what I need is to click on an mp3 file and when I hit open then I can listen to it.

Here is my code for my file chooser.

  JFileChooser chooser = new JFileChooser();
       File F = new File("C:/");
       File namedir;
       File namepath;

       chooser.setCurrentDirectory(F);
       chooser.showOpenDialog(null);
       chooser.setDialogTitle("Choose file to play");
       chooser.setApproveButtonText("Play");
  namedir = chooser.getCurrentDirectory();
  namepath = chooser.getSelectedFile();
  System.out.print("the name of the the directory is "+namedir.getName());
  System.out.print("the name of the the path is "+namepath.getAbsolutePath());
   String fileName=null;

1 Answer 1

3
String fileName=null;
JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
        "MP3 Files", "mp3");
    chooser.setFileFilter(filter);
    int returnVal = chooser.showOpenDialog(parent);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
       fileName=     chooser.getSelectedFile().getName();
    }
MediaPlayer mediaPlayer = new MediaPlayer(new Media(fileName));
mediaPlayer.play();

Here's the complete working code

import java.net.URL;
import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.embed.swing.JFXPanel;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Mayank Aggarwal
 */
public class MyAudio {

    public static void main(String[] args) {
        new MyAudio().start();
    }

    public void start() {

        String fileName = null;
        URL url;
        final CountDownLatch latch = new CountDownLatch(1);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new JFXPanel(); // initializes JavaFX environment
                latch.countDown();
            }
        });
        try {
            latch.await();
        } catch (InterruptedException ex) {
            Logger.getLogger(MyAudio.class.getName()).log(Level.SEVERE, null, ex);
        }
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "MP3 Files", "mp3");
        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            fileName = chooser.getSelectedFile().toURI().toString();

        }
        MediaPlayer mediaPlayer;
        mediaPlayer = new MediaPlayer(new Media(fileName));
        mediaPlayer.play();
    }
}
6
  • hey thanks for your try but it gives me an error saying that parent cannot be resolved to a variable. Apr 17, 2014 at 21:15
  • replace parent by name of the frame/panel or parent from which you are invoking the JFileChooser
    – maxx777
    Apr 17, 2014 at 21:18
  • So ok i now changed the parent name to songstoplay which is the item in my menu but I am having errors on the mediaplayer section it asks me to create a mediaplayer class. :( Apr 17, 2014 at 21:25
  • import the following: import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer;
    – maxx777
    Apr 17, 2014 at 21:27
  • i have provided the complete working code. Check out the edit
    – maxx777
    Apr 17, 2014 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.