[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

Easy Audio: Not working..?

Hi.

I'm just starting to work with audio now. The following code is throwing the exception (below the code):

//-- Code
    import java.awt.*;
    import java.util.*;
    import java.applet.AudioClip;
   
    public class AudioSystem
    {
       
        private Looper bg = new Looper();
       
       
        public AudioSystem() {}
       
        public static void main( String [] args )
        {
            try
            {
                new AudioSystem().bg.playBgAudio( "test.wav" );
            } catch ( Exception e )
            {
                e.printStackTrace( System.out );
            }
        }
       
       
        class Looper extends javax.swing.JApplet
        {
           
            private AudioClip bgSound = null;
           
           
            Looper() {}
           
            void playBgAudio( String musicFile )
            {
                if ( bgSound == null )
                {
                    bgSound = getAudioClip( getCodeBase(), musicFile );
                    bgSound.loop();
                }
            }
           
            void stopBgAudio()
            {
                if ( bgSound != null )
                {
                    bgSound.stop();
                    bgSound = null;
                }
            }
           
        }
       
    }
//-- Code


//-- Exception
java.lang.NullPointerException
      at java.applet.Applet.getCodeBase(Unknown Source)
      at AudioSystem$Looper.playBgAudio(AudioSystem.java:78)
      at AudioSystem.main(AudioSystem.java:48)
//-- Exception

I've removed the comments, etc, so I'll show you below what code is on the lines (specified in the exception):

  Line #: 78
  bgSound = getAudioClip( getCodeBase(), musicFile );

  Line #: 48
  new AudioSystem().bg.playBgAudio( "test.wav" );

The audio file "test.wav" exists in the same directory as the code; which is the only directory in the classpath. There's no errors at all, during compilation.. But this appears straight away, when I run it.

Any ideas?

Thanks!
Rob.
0
InteractiveMind
Asked:
InteractiveMind
  • 4
  • 4
1 Solution
 
CEHJCommented:
You can't call (directly or indirectly) getCodeBase as your applet has no context. Put the audio file in the same directory as the class and do

bgSound = getAudioClip(getClass().getResource(musicFile));
0
 
InteractiveMindAuthor Commented:
Hey CEHJ,

I tried that, but I'm not getting this exception:

java.lang.NullPointerException
      at java.applet.Applet.getAppletContext(Unknown Source)
      at java.applet.Applet.getAudioClip(Unknown Source)
      at AudioSystem$Looper.playBgAudio(AudioSystem.java:37)
      at AudioSystem.main(AudioSystem.java:17)


:o\

Thank you.
0
 
CEHJCommented:
Still a problem with no context unfortunately. Try doing it like this:

http://javaalmanac.com/egs/java.applet/LoadAudioApp.html
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
InteractiveMindAuthor Commented:
Okay. That works ... but I need some further help with that:

  (1) How do I specify the local file as a URL? To get it to work, I've had to move the audio file into the route of my server.

  (2) I've shortenned the code down to this (for now):

   
    import java.applet.*;
    import java.net.*;
   
   
    public class AudioSystem
    {
       
        public AudioSystem() {}
       
        public static void main( String [] args )
        {
            try
            {
               
                URL url = new URL("W:\\DARKINS\\Library\\com\\darkins\\games\\engines\\edge\\audio\\test.wav");
                AudioClip ac = Applet.newAudioClip(url);
                ac.loop();
                ac.play();
               
            } catch ( Exception e )
            {
                e.printStackTrace( System.out );
            }
        }
       
    }
   

This plays the very start of the audio, then exits (as I expected), I know that I can do a Thread#sleep(), in order to allow it to play, but I don't want to have to do this. So, my question is: Does the audio keep playing, until the instance of AudioClip is destroyed?

Thanks very much.
Rob.
0
 
InteractiveMindAuthor Commented:
In the code, the URL line should be:

   URL url = new URL("http://localhost/test.wav");

It specified the path because I just tested that.. :-)
0
 
CEHJCommented:
Put the file in the same directory as your class, then:

URL url = getClass().getResource("test.wav");

You should only have to call loop btw
0
 
InteractiveMindAuthor Commented:
Excellent! Thank you CEHJ. :)
0
 
CEHJCommented:
:-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now