8 Replies - 1453 Views - Last Post: 06 July 2012 - 09:56 AM Rate Topic: -----

#1 Allophyl  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 09-August 10

NullPointerException when trying to play audio clip

Posted 06 July 2012 - 07:48 AM

I have the following code in my program to play a chime at a specific point:
AudioClip chime = Applet.newAudioClip(this.getClass().getResource("/LZtimer/chimes.wav"));
chime.play();



When I try to run it from Eclipse, I get a NullPointerException every time.
When I compile it into a jar and run it in Windows, it plays the sound about half the time and just plain doesn't the other half
When I run the jar in OS X, attempting to play the sound just causes the program to freeze completely

What's going on here and how do I fix it?

Is This A Good Question/Topic? 0
  • +

Replies To: NullPointerException when trying to play audio clip

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: NullPointerException when trying to play audio clip

Posted 06 July 2012 - 07:55 AM

Is the wav file in LZTimer at the app root? Your best best is to put the wav files into a separate archive and include that in the archive attribute of the applet's html

mkdir sounds
copy *.wav sounds
jar cvfM sounds.jar sounds

This post has been edited by g00se: 06 July 2012 - 07:57 AM
Reason for edit:: code example

Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: NullPointerException when trying to play audio clip

Posted 06 July 2012 - 07:58 AM

I can't prescribe a fix for all of the undesirable behavior you're experiencing, but I've added a small program below that demonstrates playing a sound file in Eclipse. The sound file, beep.wav, (pick a sound file of your choosing) must be in a subdirectory of your project called "files". You can change that too, if you want, but if you maintain the general structure and provide files with correct names in locations that exist, it should work. You may be able to copy this structure to improve yours to being a more consistent performer.
import java.applet.*;
import java.net.*;

import javax.swing.*;
public class PlayASound {
    
    URL eng = this.getClass().getResource( "./files/beep.wav" );
    AudioClip snd = JApplet.newAudioClip( eng );
    public static void main(String[] args)
    {
        new PlayWavFile( "./files/beep.wav" );
        new PlayASound();
    }
    
    public PlayASound(){
        snd.play();
    }
}

Was This Post Helpful? 0
  • +
  • -

#4 Allophyl  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 09-August 10

Re: NullPointerException when trying to play audio clip

Posted 06 July 2012 - 08:02 AM

View Postg00se, on 06 July 2012 - 11:55 AM, said:

Is the wav file in LZTimer at the app root? Your best best is to put the wav files into a separate archive and include that in the archive attribute of the applet's html

mkdir sounds
copy *.wav sounds
jar cvfM sounds.jar sounds


it is in the package LZtimer
and... the "archive attribute of the applet's html"? - I'm afraid I have no idea what you're talking about.
Was This Post Helpful? -1
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: NullPointerException when trying to play audio clip

Posted 06 July 2012 - 08:09 AM

Quote

I'm afraid I have no idea what you're talking about.

http://docs.oracle.c...isc/applet.html
For an example of the archive attribute in use, see http://technojeeves....lock/clock.html

Of course, with my idea, your code would be
AudioClip chime = Applet.newAudioClip(this.getClass().getResource("/sounds/chimes.wav"));

It's a good idea to keep resources separate with an applet (at least on deployment)
What's your OS btw?

Quote

The sound file, beep.wav ... must be in a subdirectory of your project called "files".

More precisely actually, it must be in a subpackage of your source tree or it won't get deployed into any archives that are used
See http://technojeeves....va-with-eclipse

This post has been edited by g00se: 06 July 2012 - 08:11 AM

Was This Post Helpful? 1
  • +
  • -

#6 Allophyl  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 09-August 10

Re: NullPointerException when trying to play audio clip

Posted 06 July 2012 - 08:27 AM

View Postg00se, on 06 July 2012 - 12:09 PM, said:

Quote

I'm afraid I have no idea what you're talking about.

http://docs.oracle.c...isc/applet.html
For an example of the archive attribute in use, see http://technojeeves....lock/clock.html

Of course, with my idea, your code would be
AudioClip chime = Applet.newAudioClip(this.getClass().getResource("/sounds/chimes.wav"));

It's a good idea to keep resources separate with an applet (at least on deployment)
What's your OS btw?

This is a stand-alone application, not a web-applet.
I'm coding this on Windows atm, but it needs to work on every OS.

Putting the wav file in a separate package seems to have fixed the problem of getting an exception when trying to run it from Eclipse. It crashing on OS X and not playing all the time on Windows I think was due to the first line of the above code being executed every time I played the chime instead of executing it only once, something which was very careless of me - I've moved it to where it is only defined once and things seem to work fine now, on Windows, OS X and Linux
Was This Post Helpful? -1
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: NullPointerException when trying to play audio clip

Posted 06 July 2012 - 08:59 AM

Quote

This is a stand-alone application, not a web-applet.

Ah i see. Using an Applet to play sounds in an app is tempting, but should be avoided as you don't have much control. Have a look at the source of the applet i linked to for playing sounds in a better way (although of course that IS an applet)
Was This Post Helpful? 0
  • +
  • -

#8 Allophyl  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 09-August 10

Re: NullPointerException when trying to play audio clip

Posted 06 July 2012 - 09:40 AM

View Postg00se, on 06 July 2012 - 12:59 PM, said:

Quote

This is a stand-alone application, not a web-applet.

Ah i see. Using an Applet to play sounds in an app is tempting, but should be avoided as you don't have much control. Have a look at the source of the applet i linked to for playing sounds in a better way (although of course that IS an applet)

How do I access its source code?
Was This Post Helpful? -1
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: NullPointerException when trying to play audio clip

Posted 06 July 2012 - 09:56 AM

Quote

How do I access its source code?


The link to the source is on the page ( http://technojeeves....-applet-src.jar )
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1