java - 播放所选目录中的 MP3 文件

标签 java javafx

我正在尝试播放用户选择的文件夹中的歌曲。本质上,我正在使用我自己创建的队列,并且我得到了正确的路径。

在下面的代码中,我使用了一个名为 path 的 Var。路径是“C:\Users\Shaun\Downloads\TestMusic\Ed Sheeran - Shape of You.mp3”。当我将路径定义为“Ed Sheeran - Shape of You.mp3”时。有用!这告诉我,这会查看项目启动或运行的目录。

那么,如何让它播放任何给定目录中的文件?

我指的“路径”如下,“public void handlecentreButtonClick()”。

public class graphicalController implements Initializable 
{
    //GUI Decleration
    public Button centreButton;
    public Button backButton;
    public Button forwardButton;
    public ToggleButton muteToggle;
    public MenuItem loadFolder;

    //Controller Decleration
    String absolutePath;
    SongQueue q = new SongQueue();
    MediaPlayer player;

    @Override
    public void initialize(URL location, ResourceBundle resources)
    {
        centreButton.setStyle("-fx-background-image: url('/Resources/Play_Button.png')");
        centreButton.setText("");

        backButton.setStyle("-fx-background-image: url('/Resources/Back_Button.png')");
        backButton.setText("");

        forwardButton.setStyle("-fx-background-image: url('/Resources/Forward_Button.png')");
        forwardButton.setText("");

        muteToggle.setStyle("-fx-background-image: url('/Resources/ToggleSound_Button.png')");
        muteToggle.setText("");
    }

    public void handlecentreButtonClick() {
        if(!(q.isEmpty())) {
            String file = q.peek().fileName.toString();
            String path = absolutePath + "\\" + file; 
            Media song = new Media(path);
            player = new MediaPlayer(song);
            player.play();
        }
    }

    public void handleforwardButtonClick() {
        System.out.println("Hello.");
        centreButton.setText("Hello");
    }

    public void handlebackButtonClick() {
        System.out.println("Hello.");
        centreButton.setText("Hello");
    }

    public void handleLoadButtonClick() {
        DirectoryChooser directoryChooser = new DirectoryChooser();
        File selectedDirectory = directoryChooser.showDialog(null);
        absolutePath = selectedDirectory.getAbsolutePath();
        String path = absolutePath;
        loadFilesFromFolder(path);
    }

    public void loadFilesFromFolder(String path) {
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();
        while(!(q.isEmpty()))
        {
            try {Thread.sleep(500);}catch (Exception e){}
            Song j = q.pop();
        }
        int listLength = listOfFiles.length; 
        for (int k = 0; k < listLength; k++) {
            if (listOfFiles[k].isFile()) {
                String fileName = listOfFiles[k].getName();
                String fileNamePath = path + "\\" +fileName; 
                try {
                    InputStream input = new FileInputStream(new File(fileNamePath));
                    ContentHandler handler = new DefaultHandler();
                    Metadata metadata = new Metadata();
                    Parser parser = new Mp3Parser();
                    ParseContext parseCtx = new ParseContext();
                    parser.parse(input, handler, metadata, parseCtx);
                    input.close();
                    String songName = metadata.get("title");
                    String artistName = metadata.get("xmpDM:artist");
                    String albumName = metadata.get("xmpDM:genre");
                    int id = k + 1;
                    Song newSong = new Song(id, fileName, songName, artistName, albumName);
                    q.push(newSong);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (SAXException e) {
                    e.printStackTrace();
                } catch (TikaException e) {
                    e.printStackTrace();
                }
            }       
        } 
    }

}

最佳答案

使用

Media song = new Media(new File(path).toURI().toString());

但是,我强烈建议您以独立于平台的方式构建文件,而不是硬编码特定于某个特定文件系统的文件分隔符。你可以做

File path = new File(absolutePath, file);
Media song = new Media(path.toURI().toString());

关于java - 播放所选目录中的 MP3 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43620506/

相关文章:

java - 使用线程和/或任务更新 JavaFx Gui

java - 如何将 Swing JPanel 嵌入到 JavaFX 框架中?

java - 不可变对象(immutable对象)生成器

java - 无法调试 Spring Boot 应用程序。断点总是会逃逸

java - 检查 Android 上的页面是否已启动会导致崩溃吗?

java - 存储已保存文件的文件名和位置以保存对该特定文件的更改? [JavaFX]

java - 在使用 DFC 的 Documentum 中,我的文件夹迁移不断失败

java - 如何向调用者线程发出 ExecutorService 已完成任务的信号

java - 使用证书和 Kerberos 对用户进行身份验证?

java - 将 LineChart(javafx) 样式设置为子元素