JavaFX - ArrayLists are printed as empty after inserting Media Tags retrieved from audio files











up vote
0
down vote

favorite












I'm kind of a newbie in Java programming and I'm trying to develope a class that can retrieve Media Tags (such as title, artist, ...) from imported MP3 files, this way I can store them in a personal MySQL database.



I've got this problem I've been trying to solve for a couple days now, but I couldn't really find a solution online.



This is my class:



public class FileImportClass extends Application {

private static final Object obj = new Object();
private static List<File> songList;
private static ArrayList<String> title;
private static ArrayList<String> artist;
private static ArrayList<Integer> year;
private static ArrayList<String> genre;
private static ArrayList<String> duration;
private static ArrayList<String> album;
private static ArrayList<String> extension;
private static ArrayList<String> size;
private static ObservableMap<String, Object> songTags;

public static void FileImport(Node node){
title = new ArrayList<>();
artist = new ArrayList<>();
year = new ArrayList<>();
genre = new ArrayList<>();
duration = new ArrayList<>();
album = new ArrayList<>();
extension = new ArrayList<>();
size = new ArrayList<>();

FileChooser fileImport = new FileChooser();
fileImport.setTitle("Importazione di File Audio");
fileImport.getExtensionFilters().addAll(new ExtensionFilter("MP3 Files: ", "*.mp3"));
Stage currentStage = (Stage) node.getScene().getWindow();
songList = fileImport.showOpenMultipleDialog(currentStage);
if (songList.size() > 50) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Errore!");
alert.setHeaderText("Errore nell'inserimento dei file audio!");
alert.setContentText("È stato superato il massimo di 50 file audio importabili alla volta.");

alert.showAndWait();
return;
}
else{
for(int i = 0; i < songList.size(); i++) {
File file = songList.get(i);
Media mediaFile = new Media(file.toURI().toString());
double FILE_SIZE_D = file.length();
int FILE_SIZE_I = (int) FILE_SIZE_D;
int FILE_SIZE = FILE_SIZE_I / 1024;
size.add(String.valueOf(FILE_SIZE) + " kB");

final MediaPlayer mediaPlayer = new MediaPlayer(mediaFile);
mediaPlayer.setOnReady(new Runnable(){
@Override
public void run() {
songTags = mediaPlayer.getMedia().getMetadata();
title.add(songTags.get("title").toString());
artist.add(songTags.get("artist").toString());

double DURATA_MINUTI_D = mediaFile.getDuration().toSeconds() / 60;
double DURATA_MINUTI_I = (int) DURATA_MINUTI_D;
double DURATA_SECONDI_D = (DURATA_MINUTI_D - DURATA_MINUTI_I) * 60;
double DURATA_SECONDI_I = (int) DURATA_SECONDI_D;

duration.add(String.valueOf(DURATA_MINUTI_I) + " min " + DURATA_SECONDI_I + " s");
if (songTags.get("genre") != null) {
genre.add(songTags.get("genre").toString());
}
else {
genre.add("Genere sconosciuto");
}

if (songTags.get("album") != null) {
album.add(songTags.get("album").toString());
}
else {
album.add("Album sconosciuto");
}
year.add((Integer) songTags.get("year"));
synchronized(obj){
obj.notify();
}
}
});
}
}
System.out.println(title);
System.out.println(artist);
System.out.println(genre);
System.out.println(size);
System.out.println(duration);
System.out.println(album);
System.out.println(year);
}

@Override
public void start(Stage primaryStage) throws Exception {
throw new UnsupportedOperationException("Not supported yet.");
}
}


The problem is that, when I try to print the ArrayLists which contain the Tags, they aresult empty ("").
But if I try to print them from inside the mediaPlayer.setOnReady() function, then it works fine.



Is there anybody who could help me figure this out?



Thank you all in advance!










share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm kind of a newbie in Java programming and I'm trying to develope a class that can retrieve Media Tags (such as title, artist, ...) from imported MP3 files, this way I can store them in a personal MySQL database.



    I've got this problem I've been trying to solve for a couple days now, but I couldn't really find a solution online.



    This is my class:



    public class FileImportClass extends Application {

    private static final Object obj = new Object();
    private static List<File> songList;
    private static ArrayList<String> title;
    private static ArrayList<String> artist;
    private static ArrayList<Integer> year;
    private static ArrayList<String> genre;
    private static ArrayList<String> duration;
    private static ArrayList<String> album;
    private static ArrayList<String> extension;
    private static ArrayList<String> size;
    private static ObservableMap<String, Object> songTags;

    public static void FileImport(Node node){
    title = new ArrayList<>();
    artist = new ArrayList<>();
    year = new ArrayList<>();
    genre = new ArrayList<>();
    duration = new ArrayList<>();
    album = new ArrayList<>();
    extension = new ArrayList<>();
    size = new ArrayList<>();

    FileChooser fileImport = new FileChooser();
    fileImport.setTitle("Importazione di File Audio");
    fileImport.getExtensionFilters().addAll(new ExtensionFilter("MP3 Files: ", "*.mp3"));
    Stage currentStage = (Stage) node.getScene().getWindow();
    songList = fileImport.showOpenMultipleDialog(currentStage);
    if (songList.size() > 50) {
    Alert alert = new Alert(Alert.AlertType.ERROR);
    alert.setTitle("Errore!");
    alert.setHeaderText("Errore nell'inserimento dei file audio!");
    alert.setContentText("È stato superato il massimo di 50 file audio importabili alla volta.");

    alert.showAndWait();
    return;
    }
    else{
    for(int i = 0; i < songList.size(); i++) {
    File file = songList.get(i);
    Media mediaFile = new Media(file.toURI().toString());
    double FILE_SIZE_D = file.length();
    int FILE_SIZE_I = (int) FILE_SIZE_D;
    int FILE_SIZE = FILE_SIZE_I / 1024;
    size.add(String.valueOf(FILE_SIZE) + " kB");

    final MediaPlayer mediaPlayer = new MediaPlayer(mediaFile);
    mediaPlayer.setOnReady(new Runnable(){
    @Override
    public void run() {
    songTags = mediaPlayer.getMedia().getMetadata();
    title.add(songTags.get("title").toString());
    artist.add(songTags.get("artist").toString());

    double DURATA_MINUTI_D = mediaFile.getDuration().toSeconds() / 60;
    double DURATA_MINUTI_I = (int) DURATA_MINUTI_D;
    double DURATA_SECONDI_D = (DURATA_MINUTI_D - DURATA_MINUTI_I) * 60;
    double DURATA_SECONDI_I = (int) DURATA_SECONDI_D;

    duration.add(String.valueOf(DURATA_MINUTI_I) + " min " + DURATA_SECONDI_I + " s");
    if (songTags.get("genre") != null) {
    genre.add(songTags.get("genre").toString());
    }
    else {
    genre.add("Genere sconosciuto");
    }

    if (songTags.get("album") != null) {
    album.add(songTags.get("album").toString());
    }
    else {
    album.add("Album sconosciuto");
    }
    year.add((Integer) songTags.get("year"));
    synchronized(obj){
    obj.notify();
    }
    }
    });
    }
    }
    System.out.println(title);
    System.out.println(artist);
    System.out.println(genre);
    System.out.println(size);
    System.out.println(duration);
    System.out.println(album);
    System.out.println(year);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
    throw new UnsupportedOperationException("Not supported yet.");
    }
    }


    The problem is that, when I try to print the ArrayLists which contain the Tags, they aresult empty ("").
    But if I try to print them from inside the mediaPlayer.setOnReady() function, then it works fine.



    Is there anybody who could help me figure this out?



    Thank you all in advance!










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm kind of a newbie in Java programming and I'm trying to develope a class that can retrieve Media Tags (such as title, artist, ...) from imported MP3 files, this way I can store them in a personal MySQL database.



      I've got this problem I've been trying to solve for a couple days now, but I couldn't really find a solution online.



      This is my class:



      public class FileImportClass extends Application {

      private static final Object obj = new Object();
      private static List<File> songList;
      private static ArrayList<String> title;
      private static ArrayList<String> artist;
      private static ArrayList<Integer> year;
      private static ArrayList<String> genre;
      private static ArrayList<String> duration;
      private static ArrayList<String> album;
      private static ArrayList<String> extension;
      private static ArrayList<String> size;
      private static ObservableMap<String, Object> songTags;

      public static void FileImport(Node node){
      title = new ArrayList<>();
      artist = new ArrayList<>();
      year = new ArrayList<>();
      genre = new ArrayList<>();
      duration = new ArrayList<>();
      album = new ArrayList<>();
      extension = new ArrayList<>();
      size = new ArrayList<>();

      FileChooser fileImport = new FileChooser();
      fileImport.setTitle("Importazione di File Audio");
      fileImport.getExtensionFilters().addAll(new ExtensionFilter("MP3 Files: ", "*.mp3"));
      Stage currentStage = (Stage) node.getScene().getWindow();
      songList = fileImport.showOpenMultipleDialog(currentStage);
      if (songList.size() > 50) {
      Alert alert = new Alert(Alert.AlertType.ERROR);
      alert.setTitle("Errore!");
      alert.setHeaderText("Errore nell'inserimento dei file audio!");
      alert.setContentText("È stato superato il massimo di 50 file audio importabili alla volta.");

      alert.showAndWait();
      return;
      }
      else{
      for(int i = 0; i < songList.size(); i++) {
      File file = songList.get(i);
      Media mediaFile = new Media(file.toURI().toString());
      double FILE_SIZE_D = file.length();
      int FILE_SIZE_I = (int) FILE_SIZE_D;
      int FILE_SIZE = FILE_SIZE_I / 1024;
      size.add(String.valueOf(FILE_SIZE) + " kB");

      final MediaPlayer mediaPlayer = new MediaPlayer(mediaFile);
      mediaPlayer.setOnReady(new Runnable(){
      @Override
      public void run() {
      songTags = mediaPlayer.getMedia().getMetadata();
      title.add(songTags.get("title").toString());
      artist.add(songTags.get("artist").toString());

      double DURATA_MINUTI_D = mediaFile.getDuration().toSeconds() / 60;
      double DURATA_MINUTI_I = (int) DURATA_MINUTI_D;
      double DURATA_SECONDI_D = (DURATA_MINUTI_D - DURATA_MINUTI_I) * 60;
      double DURATA_SECONDI_I = (int) DURATA_SECONDI_D;

      duration.add(String.valueOf(DURATA_MINUTI_I) + " min " + DURATA_SECONDI_I + " s");
      if (songTags.get("genre") != null) {
      genre.add(songTags.get("genre").toString());
      }
      else {
      genre.add("Genere sconosciuto");
      }

      if (songTags.get("album") != null) {
      album.add(songTags.get("album").toString());
      }
      else {
      album.add("Album sconosciuto");
      }
      year.add((Integer) songTags.get("year"));
      synchronized(obj){
      obj.notify();
      }
      }
      });
      }
      }
      System.out.println(title);
      System.out.println(artist);
      System.out.println(genre);
      System.out.println(size);
      System.out.println(duration);
      System.out.println(album);
      System.out.println(year);
      }

      @Override
      public void start(Stage primaryStage) throws Exception {
      throw new UnsupportedOperationException("Not supported yet.");
      }
      }


      The problem is that, when I try to print the ArrayLists which contain the Tags, they aresult empty ("").
      But if I try to print them from inside the mediaPlayer.setOnReady() function, then it works fine.



      Is there anybody who could help me figure this out?



      Thank you all in advance!










      share|improve this question













      I'm kind of a newbie in Java programming and I'm trying to develope a class that can retrieve Media Tags (such as title, artist, ...) from imported MP3 files, this way I can store them in a personal MySQL database.



      I've got this problem I've been trying to solve for a couple days now, but I couldn't really find a solution online.



      This is my class:



      public class FileImportClass extends Application {

      private static final Object obj = new Object();
      private static List<File> songList;
      private static ArrayList<String> title;
      private static ArrayList<String> artist;
      private static ArrayList<Integer> year;
      private static ArrayList<String> genre;
      private static ArrayList<String> duration;
      private static ArrayList<String> album;
      private static ArrayList<String> extension;
      private static ArrayList<String> size;
      private static ObservableMap<String, Object> songTags;

      public static void FileImport(Node node){
      title = new ArrayList<>();
      artist = new ArrayList<>();
      year = new ArrayList<>();
      genre = new ArrayList<>();
      duration = new ArrayList<>();
      album = new ArrayList<>();
      extension = new ArrayList<>();
      size = new ArrayList<>();

      FileChooser fileImport = new FileChooser();
      fileImport.setTitle("Importazione di File Audio");
      fileImport.getExtensionFilters().addAll(new ExtensionFilter("MP3 Files: ", "*.mp3"));
      Stage currentStage = (Stage) node.getScene().getWindow();
      songList = fileImport.showOpenMultipleDialog(currentStage);
      if (songList.size() > 50) {
      Alert alert = new Alert(Alert.AlertType.ERROR);
      alert.setTitle("Errore!");
      alert.setHeaderText("Errore nell'inserimento dei file audio!");
      alert.setContentText("È stato superato il massimo di 50 file audio importabili alla volta.");

      alert.showAndWait();
      return;
      }
      else{
      for(int i = 0; i < songList.size(); i++) {
      File file = songList.get(i);
      Media mediaFile = new Media(file.toURI().toString());
      double FILE_SIZE_D = file.length();
      int FILE_SIZE_I = (int) FILE_SIZE_D;
      int FILE_SIZE = FILE_SIZE_I / 1024;
      size.add(String.valueOf(FILE_SIZE) + " kB");

      final MediaPlayer mediaPlayer = new MediaPlayer(mediaFile);
      mediaPlayer.setOnReady(new Runnable(){
      @Override
      public void run() {
      songTags = mediaPlayer.getMedia().getMetadata();
      title.add(songTags.get("title").toString());
      artist.add(songTags.get("artist").toString());

      double DURATA_MINUTI_D = mediaFile.getDuration().toSeconds() / 60;
      double DURATA_MINUTI_I = (int) DURATA_MINUTI_D;
      double DURATA_SECONDI_D = (DURATA_MINUTI_D - DURATA_MINUTI_I) * 60;
      double DURATA_SECONDI_I = (int) DURATA_SECONDI_D;

      duration.add(String.valueOf(DURATA_MINUTI_I) + " min " + DURATA_SECONDI_I + " s");
      if (songTags.get("genre") != null) {
      genre.add(songTags.get("genre").toString());
      }
      else {
      genre.add("Genere sconosciuto");
      }

      if (songTags.get("album") != null) {
      album.add(songTags.get("album").toString());
      }
      else {
      album.add("Album sconosciuto");
      }
      year.add((Integer) songTags.get("year"));
      synchronized(obj){
      obj.notify();
      }
      }
      });
      }
      }
      System.out.println(title);
      System.out.println(artist);
      System.out.println(genre);
      System.out.println(size);
      System.out.println(duration);
      System.out.println(album);
      System.out.println(year);
      }

      @Override
      public void start(Stage primaryStage) throws Exception {
      throw new UnsupportedOperationException("Not supported yet.");
      }
      }


      The problem is that, when I try to print the ArrayLists which contain the Tags, they aresult empty ("").
      But if I try to print them from inside the mediaPlayer.setOnReady() function, then it works fine.



      Is there anybody who could help me figure this out?



      Thank you all in advance!







      java audio javafx arraylist media






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 23:43









      MombeS

      32




      32
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You're adding the tags inside 'MediaPlayer.setOnReady', which gets called when the status of the MediaPlayer object changes to READY. This doesn't happen immediately after you call the method but after some unknown time. What this means is your print statements are executed before the status change events, so the lists are still empty by then.






          share|improve this answer






























            up vote
            0
            down vote













            As Gnas says, there's a synchronicity issue here. You'll have to do something like this:



            In your loop:



            for(int i = 0; i < songList.size(); i++) {
            final int finalI = i;
            ...


            At the end of the run() method:



            if (finalI == songList.size() - 1) {
            System.out.println(title);
            System.out.println(artist);
            System.out.println(genre);
            System.out.println(size);
            }





            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384242%2fjavafx-arraylists-are-printed-as-empty-after-inserting-media-tags-retrieved-fr%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote



              accepted










              You're adding the tags inside 'MediaPlayer.setOnReady', which gets called when the status of the MediaPlayer object changes to READY. This doesn't happen immediately after you call the method but after some unknown time. What this means is your print statements are executed before the status change events, so the lists are still empty by then.






              share|improve this answer



























                up vote
                0
                down vote



                accepted










                You're adding the tags inside 'MediaPlayer.setOnReady', which gets called when the status of the MediaPlayer object changes to READY. This doesn't happen immediately after you call the method but after some unknown time. What this means is your print statements are executed before the status change events, so the lists are still empty by then.






                share|improve this answer

























                  up vote
                  0
                  down vote



                  accepted







                  up vote
                  0
                  down vote



                  accepted






                  You're adding the tags inside 'MediaPlayer.setOnReady', which gets called when the status of the MediaPlayer object changes to READY. This doesn't happen immediately after you call the method but after some unknown time. What this means is your print statements are executed before the status change events, so the lists are still empty by then.






                  share|improve this answer














                  You're adding the tags inside 'MediaPlayer.setOnReady', which gets called when the status of the MediaPlayer object changes to READY. This doesn't happen immediately after you call the method but after some unknown time. What this means is your print statements are executed before the status change events, so the lists are still empty by then.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 at 0:13

























                  answered Nov 20 at 0:04









                  Gnas

                  47529




                  47529
























                      up vote
                      0
                      down vote













                      As Gnas says, there's a synchronicity issue here. You'll have to do something like this:



                      In your loop:



                      for(int i = 0; i < songList.size(); i++) {
                      final int finalI = i;
                      ...


                      At the end of the run() method:



                      if (finalI == songList.size() - 1) {
                      System.out.println(title);
                      System.out.println(artist);
                      System.out.println(genre);
                      System.out.println(size);
                      }





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        As Gnas says, there's a synchronicity issue here. You'll have to do something like this:



                        In your loop:



                        for(int i = 0; i < songList.size(); i++) {
                        final int finalI = i;
                        ...


                        At the end of the run() method:



                        if (finalI == songList.size() - 1) {
                        System.out.println(title);
                        System.out.println(artist);
                        System.out.println(genre);
                        System.out.println(size);
                        }





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          As Gnas says, there's a synchronicity issue here. You'll have to do something like this:



                          In your loop:



                          for(int i = 0; i < songList.size(); i++) {
                          final int finalI = i;
                          ...


                          At the end of the run() method:



                          if (finalI == songList.size() - 1) {
                          System.out.println(title);
                          System.out.println(artist);
                          System.out.println(genre);
                          System.out.println(size);
                          }





                          share|improve this answer












                          As Gnas says, there's a synchronicity issue here. You'll have to do something like this:



                          In your loop:



                          for(int i = 0; i < songList.size(); i++) {
                          final int finalI = i;
                          ...


                          At the end of the run() method:



                          if (finalI == songList.size() - 1) {
                          System.out.println(title);
                          System.out.println(artist);
                          System.out.println(genre);
                          System.out.println(size);
                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 20 at 0:17









                          Perdi Estaquel

                          616519




                          616519






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384242%2fjavafx-arraylists-are-printed-as-empty-after-inserting-media-tags-retrieved-fr%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Costa Masnaga

                              Fotorealismo

                              Sidney Franklin