簡體   English   中英

從所選目錄播放MP3文件

[英]Playing a MP3 File from a chosen Directory

我正在嘗試從用戶選擇的文件夾中播放歌曲。 本質上,我使用的是自己創建的Queue,並且獲得了正確的路徑。

在下面的代碼中,我使用的是稱為路徑的Var。 路徑為“ C:\\ Users \\ Shaun \\ Downloads \\ TestMusic \\ Ed Sheeran-Shape of You.mp3”。 當我將路徑定義為“ Ed Sheeran-Shape of You.mp3”時。 有用! 這告訴我,它查找了啟動或運行項目的目錄。

那么,如何使它從任何給定目錄播放文件?

我指的是“公共無效的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());

在@James_D的幫助下...

你不能:

媒體歌曲=新媒體(“ C:\\ Users \\ Shaun \\ Downloads \\ TestMusic \\ Ed Sheeran-Shape of You.mp3”);

這將嘗試從啟動程序的位置開始查找目錄。

做:

媒體歌曲=新媒體(新File(path).toURI()。toString());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM