我正在尝试用Java创建一个MP3播放器(目前只有控制台)。我已经在一个文本文件中存储了轨道详细信息(名称,艺术家,长度,流派,轨道路径,例如" /Users/harvhead/Desktop/music.txt")的列表,并使用了方法访问()在我的PlayMusic类中将所有轨道细节放入单独的ArrayLists中。然后我将此类继承到RockMusic类并创建了一个方法来查找任何Rock音乐(流派),然后将轨道路径放入FileInputStream然后播放Rock Mp3。问题是即使路径正确传递我收到一个FileNotFoundException(没有这样的文件或目录)。我做错了什么。请帮助我。我的代码在下面......我花了好几个小时撞在墙上试图解决这个问题。
public class PlayMusic {
List<String> trackName = new ArrayList<String>();
List<String> artist = new ArrayList<String>();
List<String> length = new ArrayList<String>();
List<String> genre = new ArrayList<String>();
List<String> ID = new ArrayList<String>();
List<String> IDtrack = new ArrayList<String>();
List<String> trackPath = new ArrayList<String>();
File f = new File("/Users/harvhead/Desktop/music.txt");
File t = new File("/Users/harvhead/Desktop/musicTracks.txt");
public void access() throws FileNotFoundException {
Scanner sc = new Scanner(f);
try {
//读取每一行文本并将第一个第二个第三个第四个元素放入不同的字符串Arraylist
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] details = line.split(",");
String trackN = details[0];
String artistN = details[1];
String trackLength = details[2];
String genreN = details[3];
String IDN = details[4];
String Pathtrack = details[5];
trackName.add(trackN);
artist.add(artistN);
length.add(trackLength);
genre.add(genreN);
ID.add(IDN);
trackPath.add(Pathtrack);
}
} catch (Exception e) {
System.out.println("Playlist Error");
}
if (trackName.isEmpty()) {
System.out.println("Arraylist is Empty");
} else {
System.out.println("ArrayList is not Empty");
}
}
以下是我的RockMusic类的方法:
public void rockPlayMusic() throws FileNotFoundException {
// call the access method (from PlayMusic Class) to create arraylists from txt file
access();
// finding Rock tracks and then trying to play them
for (int x = 0; x < genre.size(); x++) {
if (genre.get(x).contains("Rock")) {
try {
FileInputStream fis = new FileInputStream(trackPath.get(x));
System.out.println(trackPath.get(x));
Player playMP3 = new Player(fis);
playMP3.play();
} catch (Exception e) {
System.out.println(e);
}
}
}
}
以下是我的主要内容:
package musicapp;
import java.io.FileNotFoundException;
public class MusicApp {
public static void main(String[] args) throws FileNotFoundException {
rockMusic rock = new rockMusic();
rock.rockPlayMusic();
}
}
以下是我在控制台中的错误:
ArrayList is not Empty
java.io.FileNotFoundException: "/Users/harvhead/Desktop/San Tropez.mp3" (No such file or directory)
java.io.FileNotFoundException: "/Users/harvhead/Desktop/Slave To The Wage.mp3" (No such file or directory)
java.io.FileNotFoundException: "/Users/harvhead/Desktop/Six Shooter.mp3" (No such file or directory)
java.io.FileNotFoundException: "/Users/harvhead/Desktop/Bones.mp3" (No such file or directory)
BUILD SUCCESSFUL (total time: 0 seconds)
这是手动将文件路径传递到FileInputStream的代码,它播放mp3(我还可以在控制台中打印文件路径)。
public void manualPlayTest(){
try {
FileInputStream fis = new FileInputStream("/Users/harvhead/Desktop/San Tropez.mp3");
System.out.println("/Users/harvhead/Desktop/San Tropez.mp3");
Player playMP3 = new Player(fis);
playMP3.play();
} catch (Exception e) {
System.out.println(e);
}
}
硬编码到rockPlayMusic方法的路径,这很好。
public void rockPlayMusic() throws FileNotFoundException {
// call the access method (from PlayMusic Class) to create arraylists from txt file
access();
// finding Rock tracks and then trying to play them
for (int x = 0; x < genre.size(); x++) {
if (genre.get(x).contains("Rock")) {
try {
FileInputStream fis = new FileInputStream("/Users/harvhead/Desktop/San Tropez.mp3");
System.out.println(trackPath.get(x));
Player playMP3 = new Player(fis);
playMP3.play();
} catch (Exception e) {
System.out.println(e);
}
}
}
}
console output from hardcoded path in rockMusicPLay() method