Duration in seconds always returns 0

284 Views Asked by At

My main class contains these lines of code. (There is also a JButton that adds files to the playlist)

import java.util.List;
import java.util.ArrayList;
import javafx.embed.swing.JFXPanel;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;


public class Main {
  public static void main(String[] args){
    MusicPlayerGUI m = new MusicPlayerGUI();
    m.setSize(100,100);
    m.setDefaultCloseOperation(EXIT_ON_CLOSE);
    m.setVisible(true);
 }
}

class MusicPlayerGUI extends JFrame{
static DefaultListModel<String>  model  =  new  DefaultListModel<>();
int currentTrack = 0;
int count=0;
int status = 0;
List<Media> playlist  = new ArrayList<>();
JFXPanel jfx = new JFXPanel();
MediaPlayer mp; 
javax.swing.JButton btnAdd = new javax.swing.JButton("add");

public MusicPlayerGUI() {
    add(btnAdd);
    btnAdd.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnAddActionPerformed(evt);
        }
    });

} 
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
    PlaylistManager pm = new PlaylistManager();
    JFileChooser loader = new JFileChooser();
    loader.showOpenDialog(this);
    pm.addSongToPlaylist(loader.getSelectedFile());
}

}

This is another class, PlaylistManager, that manages my playlist - operations like add, remove files from playlist happen here.

public class PlaylistManager extends MusicPlayerGUI{

  public PlaylistManager(){};

  public void addSongToPlaylist(File file){

    Media songToBeAdded = new Media(file.toURI().toString());
    playlist.add(songToBeAdded); //effectively adds a song to the playlist
    MusicPlayerGUI.model.addElement(file.getName());  //adds to the visual model of the playlist (as am UI element)
    System.out.println("Added song: " + file.getName()+" with duration of " +(int)(songToBeAdded.getDuration()).toSeconds());
    count++; //increments count of songs in playlist each time one is added
}

The problem is that, every time I add a song to the playlist, the duration is always read as 0. The console prints:

Added song: name_of_the_song.mp3 with duration of 0

Why does it happen like that? What's the reason why it can't read duration properly (javafx.util.duration) ? And how can I fix that ?

1

There are 1 best solutions below

0
On

The problem is that, everytime i add a song to the playlist, the duration is always read as 0.

As per you code implementation (since, I'm not sure what file and what is the duration it has)

You initialized current track and count

int currentTrack = 0;
int count=0;

But, whenever you are adding track, you are just incrementing count not currentTrack

System.out.println("Added song: " + file.getName()+" with duration of " +(int)(playlist.get(currentTrack).getDuration()).toSeconds());

I'm not sure, why are incrementing count and not currentTrack, although you are using currentTrack to get the duration of current track. So, after each iteration of adding file your will growing, but you are still print first track duration as currentTrack is still 0.

Now, I'm assuming the duration of first track is 0, if not, then you should check verify your code and added file for the same.