2017-04-25 52 views
0

我試圖從用戶選擇的文件夾播放歌曲。基本上,我使用我創建的自己的隊列,並且正在獲得正確的路徑。從選定的目錄中播放MP3文件

在下面的代碼中,我使用了一個Var稱爲路徑。路徑是「C:\ Users \ Shaun \ Downloads \ TestMusic \ Ed Sheeran - You.mp3的形狀」。當我將路徑定義爲「Ed Sheeran - 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(); 
       } 
      }  
     } 
    } 

} 
+1

給出一個有效的文件路徑,你會創建一個新的'文件(路徑)相應的URL表示.toURI()。的toString()',即您做'歌=新媒體(新文件(路徑).toURI()的toString());'。很顯然,使用'path = absolutePath +'\\「+ file;'會在大多數文件系統(基本上除了Windows以外的任何東西)上窒息。 –

+0

謝謝James。它的確行得通了。謝謝你的提示!我認爲這很簡單,我一直堅持這一個多小時... –

回答

1

使用

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

我強烈建議您構造文件獨立於平臺的方式,但是,而不是硬編碼具體到一個特定的文件系統中的文件分隔符。你可以做

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

隨着@James_D的幫助...

你不能:

媒體歌=新媒體(「C:\用戶\肖恩\下載\ TestMusic \紅髮艾德 - You.mp3的形狀「);

這將嘗試從您啓動程序的位置找到目錄。

務必:

媒體歌=新媒體(。新的文件(路徑).toURI()的toString());

+0

「這將試圖從你啓動程序的地方找到目錄」,根本不是這樣。 [文檔](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/media/Media.html)構造函數期待一個URL(根本不是文件),所以它會失敗因爲它試圖將'C:'解釋爲一個URL方案(它顯然不是一個有效的方案)。如果你沒有提供方案,我相信它會尋找一個相對於類路徑的資源(而不是你啓動的點該程序「),雖然行爲是無證的 –

+0

哦,對了,好了,謝謝! –