I have a some Mp3 files (song.mp3) and I want to pack all of them into a single dat file (mp3s.dat) and be able to load and play the audio from the dat file. I know its possible because you can do it with midis but i’m not sure how to do it with either. Any help would be appreciated! Thanks!
I tried to pack the Mp3s into the Mp3.dat which I succeeded in sort of anyways here is the code:
public void packAFile() {
ArrayList<File> filesToPack = new ArrayList<File>();
int amountToPack = getAmountOfFilesInFolder(signlink.findcachedir()+"/Mp3/");
File[] file = new File(signlink.findcachedir()+"/Mp3/").listFiles();
for(int i = 0; i < amountToPack; i++) {
filesToPack.add(file[i]);
try(FileOutputStream fos = new FileOutputStream(signlink.findcachedir()+"/Mp3/MP3.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos);) {
oos.writeObject(filesToPack);
oos.close();
fos.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
And this is the read packed files method:
ArrayList<File> unpackedMp3s = new ArrayList<File>();
public void readMp3Dat() {
try (FileInputStream fis = new FileInputStream(signlink.findcachedir()+"/Mp3/MP3.dat");
ObjectInputStream ois = new ObjectInputStream(fis);){
unpackedMp3s = (ArrayList) ois.readObject();
ois.close();
fis.close();
System.out.println("UnpackedMp3s = " + unpackedMp3s);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
and the method i tried to play the song with:
public void playMp3FromDat(String song) {
readMp3Dat();
for(File songToPlay : unpackedMp3s) {
if(song.equalsIgnoreCase(songToPlay.getName())) {
MP3 s = new MP3(song);
s.play();
}
System.out.println("Songs: "+songToPlay.getName());
}
}
mharner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.