Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 27

Thread: Getting audio stream to play from jar file instead of system directory?

  1. #1

    Default 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:

    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?


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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.

  3. #3

    Default 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:
    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);
    	}
    }

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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.

  5. #5

    Default Re: Getting audio stream to play from jar file instead of system directory?

    Quote Originally Posted by Sean4u View Post
    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.
    I dont get half of your terminology... Is there any way you can show me how to do it?

  6. #6
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Getting audio stream to play from jar file instead of system directory?

    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.

  7. #7

    Default Re: Getting audio stream to play from jar file instead of system directory?

    Quote Originally Posted by Sean4u View Post
    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?

  8. #8
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Getting audio stream to play from jar file instead of system directory?

    Where it says in your code:
    ClassLoader.getResourceAsStream(music)
    replace it with
    this.getClass().getClassLoader().getResourceAsStream(music)

  9. #9

    Default Re: Getting audio stream to play from jar file instead of system directory?

    Quote Originally Posted by Sean4u View Post
    Where it says in your code:
    ClassLoader.getResourceAsStream(music)
    replace it with
    this.getClass().getClassLoader().getResourceAsStream(music)
    Thanks ill try that and report back.

  10. #10

    Default Re: Getting audio stream to play from jar file instead of system directory?

    With this 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:

    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.

  11. #11

    Default Re: Getting audio stream to play from jar file instead of system directory?

    Sorry for the bump but I really need help please...

  12. #12

    Default Re: Getting audio stream to play from jar file instead of system directory?

    Bump help please D:

  13. #13
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Getting audio stream to play from jar file instead of system directory?

    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'

  14. #14

    Default Re: Getting audio stream to play from jar file instead of system directory?

    Quote Originally Posted by Sean4u View Post
    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'
    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:
    as = new AudioStream(this.getClass().getClassLoader().getResourceAsStream(music));
    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?

  15. #15

    Default Re: Getting audio stream to play from jar file instead of system directory?

    help please!

  16. #16
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Getting audio stream to play from jar file instead of system directory?

    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?

    help please!
    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.

  17. #17

    Default Re: Getting audio stream to play from jar file instead of system directory?

    Quote Originally Posted by Sean4u View Post
    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.

  18. #18
    Junior Member
    Join Date
    Sep 2011
    Location
    philadelphia
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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".
    Acoolme is an Online Marketing Software Platform And Social Community

  19. #19

    Default Re: Getting audio stream to play from jar file instead of system directory?

    Quote Originally Posted by seohulu View Post
    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:
    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);
            }
    	}
    }

  20. #20

    Default Re: Getting audio stream to play from jar file instead of system directory?

    So is there any way i can loop the sound/song?

  21. #21

    Default Re: Getting audio stream to play from jar file instead of system directory?

    Is there any1 that can help me?

  22. #22
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Getting audio stream to play from jar file instead of system directory?

    Quote Originally Posted by nivangerow View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  23. #23

    Default Re: Getting audio stream to play from jar file instead of system directory?

    Quote Originally Posted by KevinWorkman View Post
    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.

  24. #24
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Getting audio stream to play from jar file instead of system directory?

    Quote Originally Posted by nivangerow View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  25. #25

    Default Re: Getting audio stream to play from jar file instead of system directory?

    Quote Originally Posted by KevinWorkman View Post
    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.
    Last edited by nivangerow; October 4th, 2011 at 12:13 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. How to play Audio file?
    By sush in forum Object Oriented Programming
    Replies: 3
    Last Post: June 29th, 2011, 08:11 AM
  2. could not create audio stream from input stream
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: June 2nd, 2011, 02:08 AM
  3. problem with File Stream
    By amr in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 11th, 2011, 09:57 PM
  4. Object and text file stream code problmes
    By Kakashi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2011, 04:34 PM
  5. [SOLVED] Web portal accessing files on the user's system via the Java I/O stream?
    By rendy.dev in forum Web Frameworks
    Replies: 2
    Last Post: January 18th, 2010, 08:46 PM

Tags for this Thread