试图让我的playCount方法在音乐播放器中工作

时间:2014-10-19 21:31:37

标签: java

我正在尝试为我的音乐播放器制作一个playcount方法。

在我的track方法中,我将一个字段定义为playCount,然后创建了一个递增它的方法。但是当我播放一首歌时,我似乎无法在我的主要课堂上调用它。

public class Track
{
// The artist.
private String artist;
// The track's title.
private String title;
// Where the track is stored.
private String filename;

public int playCount;
/**
 * Constructor for objects of class Track.
 * @param artist The track's artist.
 * @param title The track's title.
 * @param filename The track file. 
 */
public Track(String artist, String title, String filename)
{
    setDetails(artist, title, filename);
    playCount = 0;
}

public void resetCount(){
    playCount = 0;
}

public void incrementCount(){
    playCount++;
}

这是我的主要课程,省略了一些代码。

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;

/**
 * Create a MusicOrganizer
 */
public MusicOrganizer()
{
    tracks = new ArrayList<Track>();
    player = new MusicPlayer();
    reader = new TrackReader();
    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.incrementCount(); //This is where my problem is. I just want to call the increment   method so that it adds one.
        stopPlaying();
        Track track = tracks.get(index);
        player.startPlaying(track.getFilename());
        System.out.println("Now playing: " + track.getArtist() + " - " + track.getTitle());
    }
}

我得到的是一个错误“非静态方法incrementCount()不能从静态上下文中引用。我不明白这个以及我应该如何修复它。 非常感谢任何和所有的帮助!

1 个答案:

答案 0 :(得分:0)

您应该在track实例上调用它,而不是Track类。

public void playTrack(int index)
{
    if(indexValid(index)) {
        Track track = tracks.get(index);
        stopPlaying();
        player.startPlaying(track.getFilename());
        track.incrementCount();
        System.out.println("Now playing: " + track.getArtist() + " - " + track.getTitle());
    }
}