Search code examples
javaprocessingmp3mp3agic

(Processing / Java) Converting a singular file function into a function that takes an array


I am trying to create a program that uploads multiple files and stores their name and BPM tag into an ArrayList ready for comparison between the files. I have found two functions to help me but I am unable to combine them to get the function that I need.

The first function takes a singular mp3 file and outputs its data into the console (using mp3agic library):

File file = new File(dataPath("") + "/Song.mp3");

Mp3File mp3file = new Mp3File(file.getPath());
    if (mp3file.hasId3v2Tag()) {
      ID3v2 id3v2Tag = mp3file.getId3v2Tag();
      println("Track: " + id3v2Tag.getTrack());
      println("Artist: " + id3v2Tag.getArtist());
      println("BPM: " +  id3v2Tag.getBPM());
      println("Album artist: " + id3v2Tag.getAlbumArtist());
    }

The second function takes a data path and outputs the directory containing the names and info of the files in the folder

void setup() {

  String path = "Desktop/mp3folder";

  println("Listing all filenames in a directory: ");
  String[] filenames = listFileNames(path);
  printArray(filenames);

  println("\nListing info about all files in a directory: ");
  File[] files = listFiles(path);
  for (int i = 0; i < files.length; i++) {
    File f = files[i];    
    println("Name: " + f.getName());
    println("Is directory: " + f.isDirectory())
    println("-----------------------");
  }
}

// This function returns all the files in a directory as an array of Strings  
String[] listFileNames(String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    String names[] = file.list();
    return names;
  } else {
    // If it's not a directory
    return null;
  }
}

// This function returns all the files in a directory as an array of File objects
// This is useful if you want more info about the file
File[] listFiles(String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    File[] files = file.listFiles();
    return files;
  } else {
    // If it's not a directory
    return null;
  }
}

The function I am trying to create combines the two. I need the Artist, Track and BPM from the first function to work with an array list of files from a directory.

Any guidance would be appreciated. Any advice on another way to go about it would also be appreciated.


Solution

  • One way to approach this is to use classes to encapsulate the data you want to track.

    For example, here's a simplified class that contains information about artist, track, and bpm:

    public class TrackInfo{
      private String artist;
      private String track;
      int bpm;
    }
    

    I would also take a step back, break your problem down into smaller steps, and then take those pieces on one at a time. Can you create a function that takes a File argument and prints out the MP3 data of that File?

    void printMp3Info(File file){
      // print out data about file
    }
    

    Get that working perfectly before moving on. Try calling it with hard-coded File instances before you try to use it with an ArrayList of multiple File instances.

    Then if you get stuck, you can post a MCVE along with a specific technical question. Good luck.