1

你好,我是新来的。

我目前正在使用 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;
    }
}
4

1 回答 1

0

你的课很好。在您的主要方法中,您必须从文件中读取歌曲。您没有指定要处理的歌曲格式。Mp3、哇,还是只是一个带有虚构数据的简单 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()
}
于 2013-10-19T10:56:28.143 回答