你好,我是新来的。
我目前正在使用Java制作播放列表软件。我希望Song
来自文件并将其存储在播放列表中。我应该在我的歌曲课程中放一些东西来表明它应该来自一个文件吗?我错过了什么?请回答我。
public class Song {
private Float playTime;
private String songName;
private String artist;
//getters
public Float getPlayTime() { return playTime; }
public String getSongName() { return songName; }
public String getArtist() { return artist; }
}
以下是我的播放列表课程。 (不是我的,我把它放在网上的某个地方。)
public class Playlist {
//Instance variable
private List<Song> songs; //arraylist of songs
private String playlistName; //Playlist name
public Playlist(String name) {
songs = new ArrayList<Song>(); //initalize Arraylist to hold Song type
playlistName = name;
}
//Methods:
//Returns the playlist name
public String playlistName() {
return playlistName;
}
//adds Song s to Playlist
public boolean addSong(Song s) {
return songs.add(s);
}
//return list. Note return is List<Song>.
public List<Song> getList() {
System.out.print(playlistName + ":");
for (int i = 0; i < songs.size(); i++)
System.out.print(songs.get(i));
return songs;
}
public boolean removeSong(Song s) {
return songs.remove(s);
}
public double playlistTime() {
double sum = 0;
for (int j = 0; j < songs.size(); j++)
sum = sum + songs.get(j).getPlayTime();
return sum;
}
public boolean isSongInPlaylist(String name) {
boolean x = false;
for (int i = 0; i < songs.size(); i++)
if (songs.get(i).getSongName().equalsIgnoreCase(name))
x = true;
return x;
}
public void songsByArtist(String name) {
for (int i = 0; i < songs.size(); i++)
if( songs.get(i).getArtist().equals(name))
System.out.println(songs.get(i).getArtist());
}
public boolean addSongsFrom(Playlist p) { // enter code here
boolean x = true;
for (int i = 0; i < p.getList().size(); i++)
if (p.getList().get(i).getSongName().equals(songs.get(i).getSongName()))
x = x;
else
this.addSong(p.getList().get(i));
return x;
}
public int totalSongs() {
return songs.size();
}
public String toString() {
return playlistName;
}
}
答案 0 :(得分:0)
你的课很好。在main方法中,您必须从文件中读取歌曲。您没有指定要处理的歌曲格式。 Mp3,waw,还是只是一个带有虚构数据的简单txt文件?
例如,如果您想使用MP3,可以使用MP3SPI:
File file = new File(YOUR_FILE_PATH);
AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file);
if (fileFormat instanceof TAudioFileFormat) {
Map<?, ?> properties = ((TAudioFileFormat) fileFormat).properties();
String key = "duration";
Long lengthOfFile= (Long) properties.get(key);
//get other properties, then instantiate the Song object, then call addSong()
}