Solved

How to avoid "no audio device" applet death

Posted on 2000-03-23
8
352 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 51
type mismatch (Object[] to double[] 4 33
eclipse buid path vs tomcat lib path 10 34
Java 8 to Java 6 8 20
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

809 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