This is my code that plays the audio files:
/** * Author: audioPlayer * Project: DesktopApp */ package appClass; import java.io.File; import java.io.IOException; import javax.media.*; import javax.media.format.AudioFormat; public class audioPlayer { private Player[] player; private Format in1; private Format in2; private Format out; private int filePlaying; public audioPlayer(){ } public void loadAudio(File[] file){ in1 = new AudioFormat(AudioFormat.MPEGLAYER3); in2 = new AudioFormat(AudioFormat.MPEG); out = new AudioFormat(AudioFormat.LINEAR); PlugInManager.addPlugIn( "com.sun.media.codec.audio.mp3.JavaDecoder", new Format[]{in1, in2}, new Format[]{out}, PlugInManager.CODEC ); try{ for(int i = 0; i<file.length; i++){ MediaLocator mL = new MediaLocator(file[i].toURI().toURL()); System.out.print(file[i].toURI().toURL()); //ERROR POINTS HERE player[i]= Manager.createPlayer(mL); } } catch(IOException | NoPlayerException e){ e.printStackTrace(); } } public void playSound(int fileCount){ player[fileCount].start(); filePlaying = fileCount; } public void stopSound(){ if(player[filePlaying]!=null){ player[filePlaying].stop(); } } }
and that was called by this:
aP.loadAudio(mM.getFiles());
mM is a class that gets all the audioFiles from a certain directory
here:
private String path = System.getProperty("user.home")+"\\Music"; private File[] file; public File[] getFiles(){ return file; } public String getPath(){ return path; } private FileFilter audioFiles = new FileFilter(){ @Override public boolean accept(File file) { return file.getName().endsWith(".mp3") || file.getName().endsWith(".MP3") || file.getName().endsWith(".WAV") || file.getName().endsWith(".wav"); } }; public void loadFiles(){ File folder = new File(path); file = folder.listFiles(audioFiles); }
but it keeps saying that my file is a null but when i printed whats the name of the file, it is not null it has name like a real mp3 file...
jmf.jpg
My Goal:
I want to array the Player because when it is not arrayed, and when i try to play a file, it loads the file first before playing it and it takes 1-3 seconds which is not good. I tried to do this hoping it wouldn't load and when i try to play another file it plays instantly.