-
Getting audio stream to play from jar file instead of system directory?
Is it possible? This is my code and I just cant get it to work, I followed a tutorial on youtube and it came out with this code:
Code :
package net.nivangerow.Ball;
import sun.audio.*;
import java.io.*;
public class SoundManager {
public SoundManager(String music)
{
AudioStream as;
AudioPlayer ap = AudioPlayer.player;
AudioData ad;
ContinuousAudioDataStream loop = null;
try{
as = new AudioStream(new FileInputStream(music));
ad=as.getData();
loop = new ContinuousAudioDataStream(ad);
}catch(IOException e){
System.out.println(music+" Cannot be found");
}
ap.start(loop);
}
}
It cant ever find the file.
The tutorial said i should put it in my projects workspace folder in eclipse and it seems to be different on macs, so how can i load the audio stream from the jar file?
-
Re: Getting audio stream to play from jar file instead of system directory?
You need ClassLoader.getResource() or ClassLoader.getResourceAsStream() - if you know how to make a jar file and view its contents, you'll soon get something working.
-
Re: Getting audio stream to play from jar file instead of system directory?
Where would I use that in the code i posted in the op?
[edit]
I get this error
"Cannot make a static reference to the non-static method getResourceAsStream(String) from the type ClassLoader"
With this code:
Code :
package net.nivangerow.Ball;
import sun.audio.*;
import java.io.*;
public class SoundManager {
public SoundManager(String music)
{
AudioStream as;
AudioPlayer ap = AudioPlayer.player;
AudioData ad;
ContinuousAudioDataStream loop = null;
try{
as = new AudioStream(ClassLoader.getResourceAsStream(music));
ad=as.getData();
loop = new ContinuousAudioDataStream(ad);
}catch(IOException e){
System.out.println(music+" Cannot be found");
}
ap.start(loop);
}
}
-
Re: Getting audio stream to play from jar file instead of system directory?
You would use it exactly where you used it, but - as the compiler has pointed out - it's not a static method, so you can't invoke it on a reference to the class itself. The safest way to obtain (I'm not exactly sure about this, but this is what I would do) an appropriate instance of ClassLoader would be to invoke Class.getClassLoader on the Class of the instance whose code is currently executing (I'd expect that Class to have been loaded from the same source as your sound resource). If I'm working too hard to avoid 'spoon-feeding', let me know. :D
-
Re: Getting audio stream to play from jar file instead of system directory?
Quote:
Originally Posted by
Sean4u
You would use it exactly where you used it, but - as the compiler has pointed out - it's not a static method, so you can't invoke it on a reference to the class itself. The safest way to obtain (I'm not exactly sure about this, but this is what I would do) an appropriate instance of ClassLoader would be to invoke Class.getClassLoader on the Class of the instance whose code is currently executing (I'd expect that Class to have been loaded from the same source as your sound resource). If I'm working too hard to avoid 'spoon-feeding', let me know. :D
I dont get half of your terminology... Is there any way you can show me how to do it?
-
Re: Getting audio stream to play from jar file instead of system directory?
Code :
ClassLoader.getResourceAsStream(music)
'ClassLoader' is a reference to the ClassLoader class. For this to work, getResourceAsStream() would have to be declared 'static', but it isn't. That means you have to have an instance of ClassLoader to be able to use the method. This is reasonable: to launch your application there may be more than one ClassLoader at work: one which loads classes from your jar, another which loads classes from the user's installed Java environment, perhaps one that loads third-party classes over the network. You want the ClassLoader which loaded classes from your jar, because this is the one which 'knows' where the music file is.
SoundManager is in your jar too, isn't it? While you're executing code in a method in an instance of SoundManager, 'this' will refer to the current instance of SoundManager. 'this.getClass()' will return a reference to the SoundManager class. 'this.getClass().getClassLoader()' will return a reference to the ClassLoader instance that loaded the SoundManager class. That's an instance of ClassLoader which should 'do the right thing' when you invoke '.getResourceAsStream(music)' on it. Let us know what that gets you.
-
Re: Getting audio stream to play from jar file instead of system directory?
Quote:
Originally Posted by
Sean4u
Code :
ClassLoader.getResourceAsStream(music)
'ClassLoader' is a reference to the ClassLoader class. For this to work, getResourceAsStream() would have to be declared 'static', but it isn't. That means you have to have an instance of ClassLoader to be able to use the method. This is reasonable: to launch your application there may be more than one ClassLoader at work: one which loads classes from your jar, another which loads classes from the user's installed Java environment, perhaps one that loads third-party classes over the network. You want the ClassLoader which loaded classes from your jar, because this is the one which 'knows' where the music file is.
SoundManager is in your jar too, isn't it? While you're executing code in a method in an instance of SoundManager, 'this' will refer to the current instance of SoundManager. 'this.getClass()' will return a reference to the SoundManager class. 'this.getClass().getClassLoader()' will return a reference to the ClassLoader instance that loaded the SoundManager class. That's an instance of ClassLoader which should 'do the right thing' when you invoke '.getResourceAsStream(music)' on it. Let us know what that gets you.
Im sorry that I am being a real noob right now, but I don't get what you want me to do with the instance thing...
Do you mean I should make a static object of the ClassLoader class?
-
Re: Getting audio stream to play from jar file instead of system directory?
Where it says in your code:
Code :
ClassLoader.getResourceAsStream(music)
replace it with
Code :
this.getClass().getClassLoader().getResourceAsStream(music)
-
Re: Getting audio stream to play from jar file instead of system directory?
Quote:
Originally Posted by
Sean4u
Where it says in your code:
Code :
ClassLoader.getResourceAsStream(music)
replace it with
Code :
this.getClass().getClassLoader().getResourceAsStream(music)
Thanks ill try that and report back.
-
Re: Getting audio stream to play from jar file instead of system directory?
With this code:
Code :
package net.nivangerow.Ball;
import sun.audio.*;
import java.io.*;
public class SoundManager {
public SoundManager(String music)
{
AudioStream as;
AudioPlayer ap = AudioPlayer.player;
AudioData ad;
ContinuousAudioDataStream loop = null;
try{
as = new AudioStream(this.getClass().getClassLoader().getResourceAsStream(music));
ad=as.getData();
loop = new ContinuousAudioDataStream(ad);
}catch(IOException e){
System.out.println(music+" Cannot be found");
}
ap.start(loop);
}
}
I get this error:
Code :
Exception in thread "main" java.lang.NullPointerException
at sun.audio.AudioStream.<init>(AudioStream.java:48)
at net.nivangerow.Ball.SoundManager.<init>(SoundManager.java:15)
at net.nivangerow.Ball.Level1.<init>(Level1.java:15)
at net.nivangerow.Ball.Levels.<init>(Levels.java:10)
at net.nivangerow.Ball.Game.<init>(Game.java:23)
at net.nivangerow.Ball.Ball.main(Ball.java:12)
Why? I am passing the path in and everything.. Sorry if I am being a noob I have never done this sound stuff before.
-
Re: Getting audio stream to play from jar file instead of system directory?
Sorry for the bump but I really need help :( please...
-
Re: Getting audio stream to play from jar file instead of system directory?
-
Re: Getting audio stream to play from jar file instead of system directory?
Quote:
at net.nivangerow.Ball.SoundManager.<init>(SoundManag er.java:15)
Which line is line SoundManager:15? If you've changed your code in the meantime, be sure to paste latest code, latest stack trace and identify the lines in your code which correspond to the stack trace line numbers. Also make sure that any paths/URLs you're using to identify resources in your jar file match exactly with the paths printed by 'jar tvf YourJar.jar'
-
Re: Getting audio stream to play from jar file instead of system directory?
Quote:
Originally Posted by
Sean4u
Which line is line SoundManager:15? If you've changed your code in the meantime, be sure to paste latest code, latest stack trace and identify the lines in your code which correspond to the stack trace line numbers. Also make sure that any paths/URLs you're using to identify resources in your jar file match exactly with the paths printed by 'jar tvf YourJar.jar'
Code :
package net.nivangerow.Ball;
import sun.audio.*;
import java.io.*;
public class SoundManager {
public SoundManager(String music)
{
AudioStream as;
AudioPlayer ap = AudioPlayer.player;
AudioData ad;
ContinuousAudioDataStream loop = null;
try{
as = new AudioStream(this.getClass().getClassLoader().getResourceAsStream(music));
ad=as.getData();
loop = new ContinuousAudioDataStream(ad);
}catch(IOException e){
//System.out.println(music+" Cannot be found");
System.out.println(e);
}
ap.start(loop);
}
}
Line 13:
Code :
as = new AudioStream(this.getClass().getClassLoader().getResourceAsStream(music));
Code :
Exception in thread "main" java.lang.NullPointerException
at sun.audio.AudioStream.<init>(AudioStream.java:48)
at net.nivangerow.Ball.SoundManager.<init>(SoundManager.java:13)
at net.nivangerow.Ball.Level1.<init>(Level1.java:15)
at net.nivangerow.Ball.Levels.<init>(Levels.java:10)
at net.nivangerow.Ball.Game.<init>(Game.java:23)
at net.nivangerow.Ball.Ball.main(Ball.java:12)
The latest code. Do you know whats causing the problem?
-
Re: Getting audio stream to play from jar file instead of system directory?
-
Re: Getting audio stream to play from jar file instead of system directory?
Quote:
at sun.audio.AudioStream.<init>(AudioStream.java:48)
The NPE is being thrown inside the AudioStream constructor, so the only possible cause is that getResourceAsStream is returning null. There's a 'Returns:' section in the API docs for every method - did you read ClassLoader.getResourceAsStream() to see under what circumstances it returns a null?
I was playing football with my kids in the park. You should have come down and asked me there. We can always use another player. Have you considered using liveperson.com? You'll get faster responses there.
-
Re: Getting audio stream to play from jar file instead of system directory?
Quote:
Originally Posted by
Sean4u
The NPE is being thrown inside the AudioStream constructor, so the only possible cause is that getResourceAsStream is returning null. There's a 'Returns:' section in the API docs for every method - did you read ClassLoader.getResourceAsStream() to see under what circumstances it returns a null?
I was playing football with my kids in the park. You should have come down and asked me there. We can always use another player. Have you considered using liveperson.com? You'll get faster responses there.
Sorry I cant do this live person stuff, im scared about that. Also I can't play football with you because I dont know where you live so yeah.
And you don't know the cause of the error? Im gonna check the API now.
-
Re: Getting audio stream to play from jar file instead of system directory?
Is that String music is a path to a music file in local?
because that you are using "this.getClass().getClassLoader().getResourceAsStr eam(music)", the music file must be in the local folder where your code is running.Play around with the file location and test again.
if "music" is url to remote server, then you shouldn't use "this.getClass().getClassLoader().getResourceAsStr eam".
-
Re: Getting audio stream to play from jar file instead of system directory?
Quote:
Originally Posted by
seohulu
Is that String music is a path to a music file in local?
because that you are using "this.getClass().getClassLoader().getResourceAsStr eam(music)", the music file must be in the local folder where your code is running.Play around with the file location and test again.
if "music" is url to remote server, then you shouldn't use "this.getClass().getClassLoader().getResourceAsStr eam".
Nvm, i found out how to play the sound/song, now how do I loop it?
This is my code so far:
Code :
package net.nivangerow.Ball;
import sun.audio.*;
import java.io.*;
public class SoundManager {
public SoundManager(String music)
{
try {
InputStream input = SoundManager.class.getResourceAsStream(music);
AudioStream astream = new AudioStream(input);
AudioPlayer.player.start(astream);
} catch (Exception ex) {
System.err.println(ex);
}
}
}
-
Re: Getting audio stream to play from jar file instead of system directory?
So is there any way i can loop the sound/song?
-
Re: Getting audio stream to play from jar file instead of system directory?
Is there any1 that can help me?
-
Re: Getting audio stream to play from jar file instead of system directory?
Quote:
Originally Posted by
nivangerow
Is there any1 that can help me?
Please stop bumping your thread. The people who help here are doing so in their spare time, for free, just to be nice. There are hundreds of posts here, each with an urgent user waiting on a response, and your time is not more valuable than theirs. Waiting a few hours for free help is not ridiculous.
If you want to increase your chances of getting help, I suggest you read the link in my signature on asking questions the smart way, as well as posting an SSCCE that demonstrates the problem.
-
Re: Getting audio stream to play from jar file instead of system directory?
Quote:
Originally Posted by
KevinWorkman
Please stop bumping your thread. The people who help here are doing so in their spare time, for free, just to be nice. There are hundreds of posts here, each with an urgent user waiting on a response, and your time is not more valuable than theirs. Waiting a few hours for free help is not ridiculous.
If you want to increase your chances of getting help, I suggest you read the link in my signature on asking questions the smart way, as well as posting an
SSCCE that demonstrates the problem.
There is no problem, I need to know how to loop it thats all.
-
Re: Getting audio stream to play from jar file instead of system directory?
Quote:
Originally Posted by
nivangerow
There is no problem, I need to know how to loop it thats all.
What have you tried to loop it? What did google tell you?
It seems that you mostly ignored the advice you've been given, with a simple "nevermind", so I think people might hesitate to spend more time writing things that will just be discarded.
-
Re: Getting audio stream to play from jar file instead of system directory?
Quote:
Originally Posted by
KevinWorkman
What have you tried to loop it? What did google tell you?
It seems that you mostly ignored the advice you've been given, with a simple "nevermind", so I think people might hesitate to spend more time writing things that will just be discarded.
The nvm was for playing the sound/song. Now i have to loop it. I tried next to nothing because I don't know what to type in the search box. I searched for "java play sound and loop" but that had results with sounds that weren't loaded from the jar. Also some of the sample code I saw used AudioData. I have never made sounds play in java before, im sorry. And I keep bumping this because with my experience on other forums, if you don't bump your thread is 'dead' after a few hours because it is so far down the section list thing.
[edit]
I havent tried to loop it beause i dont know how to do it.