我試圖從用戶選擇的文件夾播放歌曲。基本上,我使用我創建的自己的隊列,並且正在獲得正確的路徑。從選定的目錄中播放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();
}
}
}
}
}
給出一個有效的文件路徑,你會創建一個新的'文件(路徑)相應的URL表示.toURI()。的toString()',即您做'歌=新媒體(新文件(路徑).toURI()的toString());'。很顯然,使用'path = absolutePath +'\\「+ file;'會在大多數文件系統(基本上除了Windows以外的任何東西)上窒息。 –
謝謝James。它的確行得通了。謝謝你的提示!我認爲這很簡單,我一直堅持這一個多小時... –