Solved

How to avoid "no audio device" applet death

Posted on 2000-03-23
8
350 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
compre toata in where clue oracle 4 57
ForLoop Example 3 40
oracle 11g 23 51
how to install java on RHEL image on EC2 4 10
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

862 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

24 Experts available now in Live!

Get 1:1 Help Now