2017-04-25 26 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

哦,对了,好了,谢谢! –