Solved

How to avoid "no audio device" applet death

Posted on 2000-03-23
8
349 Views
Last Modified: 2008-03-10
I have an applet that can play a sound in response to an external event.

My code is:

AudioClip clip = applet.getAudioClip( applet.getCodeBase(), "Ding.au" );
clip.play();

If 2 instance of the applet are running or some other program, such as my mail program, plays a sound, and the timing of these events causes the clips to interfere with each other, the applet writes an error message to the Java console:

"no audio device"

After this message has appeared the next, and every subsequent, clip.play() will block forever.  The applet is hung.

This is totally un-acceptable, however I cannot find any way to determine that this has happened, nor is their any excpetion I could catch to allow my applet to recover?

Does anyone have a workaround?  Is there an open bug on the sun Java Developer Connection website that I can vote for?

Thanks,
Gerry
0
Comment
Question by:gerry99
  • 4
  • 3
8 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 2651014
hmmm

I suppose that you can implement some workaround ... (not sure)
can you post some sample code ?
0
 
LVL 1

Author Comment

by:gerry99
ID: 2651413
heyhey,

This is my code, get a sound, then play it:

AudioClip clip = applet.getAudioClip( applet.getCodeBase(), "Ding.au" );
clip.play();

To reproduce the bug, put the play call in a loop with a Thread.sleep() of 5 seconds between each time it plays the sound.  Then run 2 or more instances of this applet.  FYI, "ding" is the windows ding sound saved in AU format.

Thanks,
Gerry
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2653083
all you can do is to implement the Singleton pattern - put all your audio related code in one object that is accessible from all applets

public class AudioManager
{
  ptivate static AudioManager default = new AudioManager ();
  public static AudioManager getDefault()
  {
    return default;
  }
 
  private AudioClip currentClip;
  public void play(Applet applet, String soundFile)
  {
    if (currentClip != null)
    {
      currentClip.stop();
      currentClip = null;
    }
    if (url != null)
    {
      currentClip = applet.getAudioClip(applet.getCodeBase(), soundFile);
      clip.play();    
    }
  }
}


use it
AudioClip.getDefault().play(applet, "ding.au");
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2658209
The AudioClip.play() method hogs the audio device as I discovered. Try running your applet and something else that produces sound...like winamp or something, you will find that it will tell you that it can't play music because the sound device is not available.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 16

Expert Comment

by:heyhey_
ID: 2658862
yes - that's what my workaround is for ... :)
0
 
LVL 1

Author Comment

by:gerry99
ID: 2659548
heyhey,

Since I posted my question I implemented a workaround that has a simular effect to your suggestion of using a singleton AudioManager.  I created a thread to play audio clips, primarily so that if another program wants to use the audio device and if this causes the AudioClip.play() method to hang, it will only hang my audio playing thread, not my worker thread that I was counting on having perform some useful task.  Your singleton pattern will still hang my worker thread if I use it.

Your suggestions are useful however, I'd like to know if there is a way to detect the "no audio device" error and to somehow reset the part of the VM that plays music or at least be able to avoid making the AudioClip.play() call.  (If you can't detect the error making this call will block your thread forever.)

Thanks
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 200 total points
ID: 2660201
'standard' Java 1.1 gives you only AudioClip.play() and stop() functionality. you can probably use the functionality of  the undocumented com.sun.audio.* classes, but since audio uses a lot of native resources, the system will remain  quite unstable (because of bad OS system DLLs, bad audio drivers, etc)
0
 
LVL 1

Author Comment

by:gerry99
ID: 2662847
Thanks for the help.  My question was really un-answerable, because there is no application level way to work around a bug in the Windows-x VM AudioClip implementation.  As far as I can tell the only safe thing is to let you Audio playing thread hang, and get the rest of your applet out of the way.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now