0

我不确定我在这里做错了什么。我得到了它,您可以从列表中随机播放一首歌曲,但不能随机播放整个列表一次。我能得到的任何帮助将不胜感激。我的代码是:

import java.util.ArrayList;
import java.util.Random;

/**
 * A class to hold details of audio tracks.
 * Individual tracks may be played.
 * 
 * @author William Gambill
 * @version July 10, 2013
 */
public class MusicOrganizer
{
    // An ArrayList for storing music tracks.
    private ArrayList<Track> tracks;
    // A player for the music tracks.
    private MusicPlayer player;
    // A reader that can read music files and load them as tracks.
    private TrackReader reader;
    // Used to initialize the random feature.
    private Random randomtrack;

    /**
     * Create a MusicOrganizer
     */
    public MusicOrganizer()
    {
        tracks = new ArrayList<Track>();
        player = new MusicPlayer();
        reader = new TrackReader();
        randomtrack = new Random();
        readLibrary("audio");
        System.out.println("Music library loaded. " + getNumberOfTracks() + " tracks.");
        System.out.println();
    }

    /**
     * Add a track file to the collection.
     * @param filename The file name of the track to be added.
     */
    public void addFile(String filename)
    {
        tracks.add(new Track(filename));
    }

    /**
     * Add a track to the collection.
     * @param track The track to be added.
     */
    public void addTrack(Track track)
    {
        tracks.add(track);
    }

    /**
     * Play a track in the collection.
     * @param index The index of the track to be played.
     */
    public void playTrack(int index)
    {
        if(indexValid(index)) {
            Track track = tracks.get(index);
            player.startPlaying(track.getFilename());
            System.out.println("Now playing: " + track.getArtist() + " - " + track.getTitle());
        }
    }

    /**
     * Play a track in the collection at random.
     * @param index The index of the track to begin the randomizer.
     */
    public void randomTrack(int index)
    {
        int trackNumber = getNumberOfTracks();
        int rindex = randomtrack.nextInt(trackNumber);
        if(indexValid(index)) {
            Track track = tracks.get(rindex);
            player.startPlaying(track.getFilename());
            System.out.println("Now playing: " + track.getArtist() + " - " + track.getTitle());
        }
    }

    /**
     * Play all tracks in the collection at random.
     * @param index The index of the track to begin the randomizer.
     */
    public void randomAllTracks(int index)
    {
        int trackNumber = getNumberOfTracks();
        int rindex = randomtrack.nextInt(trackNumber);
        if(indexValid(index)) 
        {
            for(Track track : tracks)    
            {
            player.startPlaying(track.getFilename());
            System.out.println("Now playing: " + track.getArtist() + " - " +   track.getTitle());
            System.out.println();
            }


        }
    }

    /**
     * Return the number of tracks in the collection.
     * @return The number of tracks in the collection.
     */
    public int getNumberOfTracks()
    {
        return tracks.size();
    }

    /**
     * List a track from the collection.
     * @param index The index of the track to be listed.
     */
    public void listTrack(int index)
    {
        System.out.print("Track " + index + ": ");
        Track track = tracks.get(index);
        System.out.println(track.getDetails());
    }

    /**
     * Show a list of all the tracks in the collection.
     */
    public void listAllTracks()
    {
        System.out.println("Track listing: ");

        for(Track track : tracks) {
        System.out.println(track.getDetails());
        }
        System.out.println();
    }

    /**
     * List all tracks by the given artist.
     * @param artist The artist's name.
     */
    public void listByArtist(String artist)
    {
        for(Track track : tracks) {
            if(track.getArtist().contains(artist)) {
                System.out.println(track.getDetails());
            }
        }
    }

    /**
     * Remove a track from the collection.
     * @param index The index of the track to be removed.
     */
    public void removeTrack(int index)
    {
        if(indexValid(index)) {
            tracks.remove(index);
        }
    }

    /**
     * Play the first track in the collection, if there is one.
     */
    public void playFirst()
    {
        if(tracks.size() > 0) {
            player.startPlaying(tracks.get(0).getFilename());
        }
    }

    /**
     * Stop the player.
     */
    public void stopPlaying()
    {
        player.stop();
    }

    /**
     * Determine whether the given index is valid for the collection.
     * Print an error message if it is not.
     * @param index The index to be checked.
     * @return true if the index is valid, false otherwise.
     */
    private boolean indexValid(int index)
    {
        // The return value.
        // Set according to whether the index is valid or not.
        boolean valid;

        if(index < 0) {
            System.out.println("Index cannot be negative: " + index);
            valid = false;
        }
        else if(index >= tracks.size()) {
            System.out.println("Index is too large: " + index);
            valid = false;
        }
        else {
            valid = true;
        }
        return valid;
    }

    private void readLibrary(String folderName)
    {
        ArrayList<Track> tempTracks = reader.readTracks(folderName, ".mp3");

        // Put all thetracks into the organizer.
        for(Track track : tempTracks) {
            addTrack(track);
        }
    }
}
4

3 回答 3

4

您可以在代码中使用Collections.shuffle()吗?根据文档:

使用默认随机源随机排列指定列表。所有排列都以大致相等的可能性发生。

您可以洗牌并从每次洗牌中List获取第一个元素。List

于 2013-07-13T16:18:14.367 回答
3

您需要做的是,不是每次播放歌曲时都选择一个随机数,而是生成所有歌曲的列表,然后将整个列表打乱,使其按随机顺序播放,然后一次播放一首。

于 2013-07-13T16:19:40.960 回答
1

@The New Idiot 和 @SharkofMirkWood 的答案的组合是正确的。像这样的东西

public void randomAllTracks() {
    List<Track> randomList = buildRandomList();
    playList(randomList);
}

private List<Track> buildRandomList() {
    List<Track> randomList = new ArrayList<Track>(this.tracks);
    Collections.shuffle(randomList);

    return randomList;
}

public void playList(List<Track> playList) {
    if(playList.size() > 0) {
        for(Track track : playList) {
            player.startPlaying(track.getName());
        }
    }
}
于 2013-07-13T16:40:30.020 回答