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.

Results 1 to 15 of 15

Thread: Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet

    omg i thought i finished everything, i forgot the sound files for the program.
    heres a code in a method that returns an ImageIcon using Class loader and getResource(<Strig
        /** Returns an ImageIcon, or null if the path was invalid. */
        protected static ImageIcon createImageIcon(String path) {
     
            java.net.URL imgURL = HighScoreUserWindow.class.getResource(path);
     
            if (imgURL != null) {
     
                return new ImageIcon(imgURL);
            }
            else {
     
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }

    and i was attempting the same approach for locating .au files, but i just keep getting errors like i cant return the audioStream object
    public AudioStream getAudioStream(String path) {
     
            InputStream iStream = SoundEffectSample.class.getResourceAsStream(path);
            AudioStream aStream;
     
            try {
                aStream = new AudioStream(iStream);
     
     
            } catch (IOException ex) {
     
                Logger.getLogger(SoundEffectSample.class.getName()).log(Level.SEVERE, null, ex);
            }
     
            return aStream;
        }

    i was searching on google, but i cant find anything that can help me with this, i just want to know, how would this kind of approach be possible , thanks again


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet

    just keep getting errors
    Please copy and paste here the full text of the error message.
    If needed, Use printStackTrace to get the full error message.

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet

    sorry, what i mean with the error is, when i try to return an audio stream object, it cannot see the part where it was initialized(inside the try block)
    it requires me to initialize it, i dont know where to put the initializiation and the return part, im messing up

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet

    Where is the posted error message?
    I'm assuming that it is a scope problem for a variable?
    Move the definition of the variable to the same scope as where you are referencing it.
    Assign it an initial value of null.

  5. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet

      public static AudioStream getAudioStream(String path) throws IOException {
     
            InputStream iStream = SoundEffectSample.class.getResourceAsStream(path);
            AudioStream aStream = new AudioStream(iStream);;
     
     
            return aStream;
        }

    i tried to throw and exception instead of catching it, and i got these errors
    Exception in thread "main" java.lang.NullPointerException
    	at sun.audio.AudioStream.<init>(AudioStream.java:48)
    	at kkk.TestPackage.SoundEffectSample.getAudioStream(SoundEffectSample.java:43)
    	at kkk.TestPackage.SoundEffectSample.main(SoundEffectSample.java:30)
    Java Result: 1

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet

    at kkk.TestPackage.SoundEffectSample.getAudioStream(S oundEffectSample.java:43)
    Find What variable on line 43 is null?
    Then back track to see why it is null.

  7. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet

    norm im im really messed up, , maybe i should get straight, i should have asked for a simple code that will do the same thing as the method that gets an image through class loader, if you dont mind

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet

    Sorry, I don't understand. What image?
    Where are you trying to get an image?

    The ImageIO class has methods for getting images.

  9. #9
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet

    oh sorry, i think i really need to practice my english as well, what i mean is,
    a method like this that returns an AudioStream
     /** Returns an ImageIcon, or null if the path was invalid. */
        protected static ImageIcon createImageIcon(String path) {
     
            java.net.URL imgURL = MyClass.class.getResource(path);
     
            if (imgURL != null) {
     
                return new ImageIcon(imgURL);
            }
            else {
     
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }

    the images that this method is calling is on the same package/dir,
    im attempting to put the audio files on the same directory, and i want to call those audio using this kind of approach, i was researching
    i only found the use of getResourceAsStream() method but im having some problems using this kind of approach.

  10. #10
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import sun.audio.AudioData;
    import sun.audio.AudioPlayer;
    import sun.audio.AudioStream;
    import sun.audio.ContinuousAudioDataStream;
     
    import java.util.*;
     
    public class qw1 {
     
        private InputStream iStream;
        private AudioStream aStream;
     
        public void Play() {
     
            java.net.URL audioUrl = qw1.class.getClassLoader().getResource("KKKFOLDER\\samp.au");
     
            try {
     
                // if I put the location of my samp.au from drive C. it works fine
                // "C:KKKFOLDER\\samp.au" 
                iStream = new FileInputStream(audioUrl.getPath());
                aStream = new AudioStream(iStream);
            }
            catch (Exception ex) {
     
                ex.printStackTrace();
            }
     
            AudioPlayer.player.start(aStream);
        }
     
        public static void main(String[] args) {
     
            new qw1().Play();
        }
    }

    if I put on the FileInputStream the path from "C:\\KKKFOLDER\\samp.au" drive it works fine
    but when i use the URL it cannot locate from "C:\\KKKFOLDER\\samp.au" or "KKKFOLDER\\samp.au", it throws me a null pointer exception,
    the audio file is located on C drive and on the same package where my classes resides

    i tried this one by getting the resource as stream, hoping that it will get the file and return it as an InputStream object
    public class qw1 {
     
        private InputStream iStream;
        private AudioStream aStream;
     
        public void Play() {
     
            try {
     
                iStream = qw1.class.getResourceAsStream("KKKFOLDER\\samp.au");
                aStream = new AudioStream(iStream);
            }
            catch (Exception ex) {
     
                ex.printStackTrace();
            }
     
            AudioPlayer.player.start(aStream);
        }
     
        public static void main(String[] args) {
     
            new qw1().Play();
        }
    }

    i always get a null object
    java.lang.NullPointerException
    	at sun.audio.AudioStream.<init>(AudioStream.java:48)
    	at kkk.TestPackage.qw1.Play(qw1.java:35)
    	at kkk.TestPackage.qw1.main(qw1.java:47)
    Last edited by chronoz13; July 9th, 2011 at 02:26 AM.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet

    Looks like a path problem. If your code is in a package, you have to consider that.
    Try more combinations of paths until getResourceAsStream finds the file.

    Write a small test program that gets an InputStream to a text file and use Scanner to read the file and print its contents. Make many versions of the file with different contents and put them in all the possible combinations of folders. Execute the program. When it reads the file and displays the contents you will know by looking at what is displayed which file was read. Then make changes to the path and to the package statement and do it again. You should see which package and path go together for the program to be able to find and read the file.

  12. The Following User Says Thank You to Norm For This Useful Post:

    chronoz13 (July 9th, 2011)

  13. #12
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet

    finally!! i solved it! and yes and thank you! thats what i did, i tried different combinations , file names, file paths, file location and even the code it self


    public static void playMusic() {
     
            try {
     
                URL url = MainBackgroundSound.class.getResource("fileName.file");
                audioPlayer = Manager.createRealizedPlayer(url);
            } 
            catch (Exception ex) {
     
                ex.printStackTrace();
            }
     
            audioPlayer.start();
        }

    public void Play() {
     
            try {
     
                // make sure that this audio file is at the same package as 
                // this class is
                iStream = SoundAuClassLoaderSample.class.getResourceAsStream("ExternalFilesSoundAndImages\\samp.au");
                aStream = new AudioStream(iStream);
            }
            catch (Exception ex) {
     
                ex.printStackTrace();
            }
     
            AudioPlayer.player.start(aStream);
        }
    these is where i ended up, either using sun.audio package or javax.media. i manage to get the file through getting it from a URL class loader or as an input stream. finnaly, i can manage to organize and locate my images and sound files in a better way, thanks again for your time norm !

  14. #13
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet

    oh and by the way, thanks for giving me this additional knowledge
    Write a small test program that gets an InputStream to a text file and use Scanner to read the file and print its contents.

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet

    Doing it yourself with a small test program will often be a faster way to find a solution than posting a big program here and waiting for someone to make suggestions.

  16. #15
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet

    well sometimes while solving a programmatic problem while waiting and having suggestions like this, will guide you in straighter ideas, than messing up with bunch of headache giving-more thoughts, well i really appreciate everything i got from you mates, another accomplishment
    Last edited by chronoz13; July 9th, 2011 at 11:18 AM.

Similar Threads

  1. Looping an inputstream
    By Trunk Monkeey in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 25th, 2011, 02:39 PM
  2. [SOLVED] InputStream to File
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: May 2nd, 2011, 01:05 PM
  3. getTimeInMillis() from the Calendar class returns wrong values
    By 16mydream in forum Java Theory & Questions
    Replies: 2
    Last Post: February 16th, 2011, 01:29 PM
  4. InputStream Problem at Client Side
    By pavan in forum Web Frameworks
    Replies: 1
    Last Post: March 26th, 2010, 03:21 AM
  5. Accessing a method of one class in another class
    By Sai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2010, 04:06 PM