Solved

How to avoid "no audio device" applet death

Posted on 2000-03-23
8
354 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java basic valueOf question 1 50
What browser will run Java? 7 172
ArrayList Adding/settign data 3 37
Strange router problem - can't access hotmail.com 14 108
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

734 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