Audio does not play in Jar but does in eclipse - java

Sound does not play when I run the JAR, but it does when I run it in eclipse.
Here is where I load the clips:
public void init(){
System.out.println("grabbing Music");
String currentDir = new File("").getAbsolutePath();
name=new File(currentDir+"\\music\\").list();
clip=new Clip[name.length];
soundFile=new File[name.length];
for(int x=0;x<name.length;x++){
System.out.println(currentDir+"\\music\\"+name[x]);
try {
soundFile[x]= new File(currentDir+"\\music\\"+name[x]);
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile[x]);
DataLine.Info info= new DataLine.Info(Clip.class, sound.getFormat());
clip[x] = (Clip) AudioSystem.getLine(info);
clip[x].open(sound);
clip[x].addLineListener(new LineListener(){
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
event.getLine().close();
}
}
});
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
}
}
}
I do not get any errors when running it in Eclipse. There should be no possibility of an invalid directory error, so what is wrong?
-When the jar is run in CMD i get no errors.
edit: I feel like I am loading the audio wrong, hence why I pasted the code I used to load the files in. In my searches I haven't seen anyone use File to load in a sound file. Wonder if that is the problem?

First thing that goes into my mind is that you didn't attached your sound library classes into your jar.

In order to run your current code, the folder music should be in the same folder the jar file is located in.
Another solution is to package your music folder inside the jar file and then change your code to:
InputStream is = getClass().getResourceAsStream("/music/" + name[x]);
AudioInputStream sound = AudioSystem.getAudioInputStream(is);

How about-
Right click on your project in Eclipse. Then New -> Source Folder.
Name the source folder anything. e.g. music_src.
Copy or drag the entire music directory in music_src. Then make the jar.

File systems have a hard time looking into jars.
Try using URL instead. A URL can locate a location within a jar. This happens a lot with folks trying to access resources in jars for the first time.
Otherwise things look fine.

Related

Play Asset Delivery not working when fetching pdfs

So I have this app, but its to big for the play store because it has lots of pdfs, so I decided to use play asset delivery to retrieve the pdfs. I have done everything like it says in google docs, I have created an asset-package, changed everything in the manifest, build.gradle and build a bundle! But the play asset delivery just isn't working! I can't get my pdf files that are on the "pdfs->src->main->assets" !! Can someone please help me? Am I choosing the right package name?
My source code to retrieve the pdf is the following :
mPDFView = (PDFView) findViewById(R.id.pdf);
Context context = null;
try {
context = createPackageContext(getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
AssetManager assetManager = context.getAssets();
try {
InputStream is = assetManager.open("A abóboda.pdf");
mPDFView.fromStream(is).load();
} catch (IOException e) {
e.printStackTrace();
}

How To Play A Local Sound File

I want to make a program that plays sounds and displays png images onto the JFrame. I am trying to put the png and sound files (.wav) into the package that the class that's displaying it is in. I can't seem to get it working though. I've looked up many methods on how to do it, it every time they all pop up NullPointer errors. Or that it couldn't find the file, even though the file path specified was exactly where it was when I went into File Explorer. So if anyone can help me find a way to play music and display the picture (getting the png file and making it an ImageIcon), that would be great.
Here is java code
Play Button action Performed
private void play_btnActionPerformed(java.awt.event.ActionEvent evt) {
SetImage();
PlaySound();
}
Play audio
void PlaySound() {
try (InputStream in = getClass().getResourceAsStream("sam.wav")) {
InputStream bufferedInS = new BufferedInputStream(in);
try (AudioInputStream audioInS = AudioSystem.getAudioInputStream(bufferedInS)) {
Clip clip = AudioSystem.getClip();
clip.open(audioInS);
clip.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
set image
void SetImage() {
audio_icon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/player/player.png")));
}

Playing a wav file in Java after compiling it to JAR

so I am making a birthday present for a programmer friend of mine. I am not that good with code but I made a window + gif + sound. But once I test it on another PC the sound won't work anymore, but the JAR file is big enough to contain the WAV. PLease help me, I really want to make a nice birthday gift. Here is the Sound code + main
public static void play() {
try {
File file = new File("C:/Users/timma/IdeaProjects/BirthdayAshley/1" + ".wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(file));
clip.start();
Thread.sleep(clip.getMicrosecondLength());
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
public static void main(String[] args){
JFrame jf = new JFrame ("Happy Birthday");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(617,345);
jf.add(new Birthday());
jf.setVisible(true);
jf.setResizable(false);
play();
}
If the WAV is inside a JAR, it can't be referenced via a file.
But you can open it via Class.getResourceAsStream(). Everything else should stay the same.
InputStream stream =
SomeClassInTheSameJar.class.getResourceAsStream("/BirthdayAshley/1.wav");
use <MyClass>.class().getAsStream("/1.wav") to load the file. So it will loaded from your jar not with an absolut Path that not exists.
clip.open(AudioSystem.getAudioInputStream(<MyClass>.class.getAsStream("/1.wav")));

Android Studio path

Hey Guys I did a little App, where I type into a textbox a specific value (height, weight) and save it into a file.
I did this but I do not know, which path I have to use for Android.
Hope you can help :)
public void SaveList(View view) {
//Pf`enter code here`ad, im privaten Speicherbereich
File file = new File("I need this path :)");
try {
OutputStreamWriter fdg = new OutputStreamWriter(new FileOutputStream(file));
fdg.write(""+this.weight);
fdg.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
You can use Environment.getDataDirectory() to get the root directory, if you dont have an SD card.
If you have an SD card, use Environment.getExternalStorageState()
Read more about them in the docs
Thus, change your code as follows
File file = new File(Environment.getDataDirectory()+"/your_folder_name/your_file_name");
This will create a file with name your_file_name in the folder your_folder_name in your internal storage.

Clean and build, but my audio file is gone

I have a little question, I am building an app with the swing builder in Netbeans(note: it is Java). In this app I use an audio file I put in the main project folder, but when I start the jar, the audio file is not working, as if it's not included.
Does anyone know how I can fix this?
Any help will be greatly appreciated,
Black Magic
make sure that you have audio files in folder with jar and that path is correct. I used this method to play sounds:
public static synchronized void playSound(final File file)
{
new Thread(new Runnable()
{
#Override
public void run()
{
try
{
AudioInputStream inputStream = AudioSystem.getAudioInputStream(file);
Clip clip = AudioSystem.getClip();
clip.open(inputStream);
clip.start();
} catch (Exception e)
{
System.err.println(e.getMessage());
}
}
}
)
.start();
}
In code i play sound using: playSound(new File("sounds/noise.wav"));
So i place my folder with noise.wav in folder with *.jar and it all works.

Categories

Resources