Question

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;
Was it helpful?

Solution

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();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top