我试图从用户选择的文件夹播放歌曲。基本上,我使用我创建的自己的队列,并且正在获得正确的路径。从选定的目录中播放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。它的确行得通了。谢谢你的提示!我认为这很简单,我一直坚持这一个多小时... –