简体   繁体   中英

Error with code for playlists in java mp3 player

I'm struggling with creating a playlist for my simple mp3 player in Java but the code is somehow not working. Here is the playlist part:

FileFilter filter = new FileNameExtensionFilter("MP3 Files", "mp3", "mpeg3");
JFileChooser chooser = new JFileChooser("C:\\Users\\Junior\\Music");
chooser.addChoosableFileFilter(filter);
chooser.setMultiSelectionEnabled(true);

int returnVal = chooser.showOpenDialog(null);

if(returnVal == JFileChooser.APPROVE_OPTION){
    //File myFile=chooser.getSelectedFile();
    File[] files=chooser.getSelectedFiles();
    //String song=myFile+"";
    for(int i=0; i<files.length;i++){
    name=files[i].getName()+"";//getting song name for display purpose
    try {
        MC.Stop();//stopping if any file is playing

        display.setText(name.substring(0,name.length()-4));//setting name songs in display (without .mp3)


        MC.Play(files[i]+"");//from main class i'm calling play method
    } catch (IOException e) {//i'm not really catching any exceptions so...

    }//end of catch
    }//end of for-loop
}//end of if

This code is playing the last song selected (even if I choose multiple files - so the last selected song) but not advancing to the next song.

I thought this might help you from reading your comment. This is a playlist class that I created. It's an abstract class, so you need to create your own class that extends this class, and implement the loadSongs method

public abstract class Playlist {

    public static final int LOOP_NONE = 0;
    public static final int LOOP_PLAYLIST = 1;
    public static final int LOOP_SONG = 2;

    private List<File> songs;
    private int playlistPos;
    private int loopMode;
    private boolean isShuffled;


    public Playlist() {
        isShuffled = false;
        loopMode = 0;
    }


    protected abstract List<File> loadPlaylist();


    public void initPlaylist() {
        songs = loadPlaylist();
    }


    public File getCurrentSong() {
        return songs.get(playlistPos);
    }


    public int getPlaylistPosition() {
        return playlistPos;
    }


    public void setPlaylistPosition(int playlistPos) {
        this.playlistPos = playlistPos;
    }


    public int getPlaylistSize() {
        return songs.size();
    }


    public boolean isFinalSong() {
        return playlistPos == songs.size() - 1;
    }


    public boolean isShuffled() {
        return isShuffled;
    }


    public int getLoopMode() {
        return loopMode;
    }


    public void toggleShuffle() {
        setShuffle(!isShuffled);
    }


    public void toggleLoop() {
        loopMode = (loopMode + 1) % 3;
    }


    public void skipForward() {
        playlistPos = (playlistPos + 1) % songs.size();
        // Re-shuffle songs each time the playlist loops
        if (playlistPos == 0 && isShuffled) {
            shuffle();
        }
    }


    public void skipBack() {
        playlistPos = playlistPos == 0 ? songs.size() - 1 : playlistPos - 1;
    }


    public void setLoop(int loopMode) {
        this.loopMode = loopMode;
    }


    public void setShuffle(boolean shuffle) {
        this.isShuffled = shuffle;
        if (isShuffled) {
            shuffle();
        }
        else {
            unshuffle();
        }
    }


    protected void shuffle() {
        Collections.shuffle(songs.subList(playlistPos + 1, songs.size()));
    }


    protected void unshuffle() {
        initPlaylist();
    }

}

So you would create a subclass like this:

public TestPlaylist extends Playlist {

    @Override
    protected List<File> loadPlaylist() {
        // Show your file chooser, and return the selected files
        // as a list.
    }
}

And to use it, you would write:

Playlist playlist = new TestPlaylist();
playlist.initPlaylist();

// Do what you want with it, for example:
playlist.setShuffle(true);
MC.play(playlist.getCurrentSong().getAbsolutePath());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM