0

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.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
Z Iqbal
  • 59
  • 7
  • it's not clear, what do you want to combine? – Ryuzaki L Aug 06 '18 at 21:50
  • This isn't a site for doing your homework for you. It's a site for asking far more pointed questions. If you want someone to actually write your code for you, look for a site that rents coders. What you should do is give the problem a try then post what you've tried and what is failing. – Joseph Larson Aug 06 '18 at 21:56
  • @JosephLarson I did put my attempt at coding the function previously but I was suggested to edit it out for whatever reason. I'm not trying to get people to do my homework I am simply asking for guidance. – Z Iqbal Aug 06 '18 at 22:45

1 Answers1

1

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.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • 1
    Thanks Kevin for clarifying that. I will take on board your advice and the code you have provided. Thanks again for answering most of my questions btw. – Z Iqbal Aug 07 '18 at 00:52