Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

play sounds WITHOUT locking out other apps?

0 views
Skip to first unread message

Eric Osman

unread,
Jun 10, 2001, 9:15:13 AM6/10/01
to

Is there a way to play an occasional sound from java such that
OTHER APPLICATIONS can still play their own sounds ?

Whenever I use JApplet.getAudioClip , my system applications (not
java ones) suddenly can not play their own sounds.

NOTE: I wouldn't mind if getAudioClip locked the resource as
long as there were a way to RELEASE the audio clip
after playing my sound, but I can't find any such
method.

Thanks for any ideas ! /Eric

p.s. I'm on windows 98 second edition

Ken Kalish

unread,
Jun 10, 2001, 6:22:14 PM6/10/01
to

This works on 98 with 1.2.2, Eric:
clip.stop();

...and the sound card is released

>p.s. I'm on windows 98 second edition

--

Regards,

Ken Kalish

there is no Java cartel

Eric Osman

unread,
Jun 11, 2001, 12:03:05 AM6/11/01
to

Why doesn't the following program work, according to your
suggestion ?

Here's my test:

1) bring up "sounds" system control panel and play any
of the system sounds to verify that they play
2) Bring up the below program as an applet in netscape,
and click the "play" button to verify that the program
plays its sound.
3) Attempt to play a system sounds in the sounds system
control panel again. I get an ERROR saying that
sound card is in use. It's this error I'm trying
to solve.

Thanks. /Eric

Here's the program. Please launch it as an applet and try the
above test, and let me know what I need to do to allow the
system sounds to be available between clicks of the "play" button
in this applet !

//////////////////////////////////////////////////////////////////////////
//
// Demo to play audio in java as an applet
//
// Creation: Eric Osman, os7...@mediaone.net , 6/8/01
//
//////////////////////////////////////////////////////////////////////////

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PlayAudio extends JApplet {

public int width=300;
public int height=200;

public AudioClip sound;
public JButton playButton;


//////////////////////////////////////////////////////////////////////////
//
// When applet starts, give user mini-control panel
//

//////////////////////////////////////////////////////////////////////////
public void init () {
Container c = getContentPane();
JPanel jPanel = new JPanel();
jPanel.setSize(width, height);
c.setLayout(new FlowLayout());
ButtonHandler handler = new ButtonHandler();

playButton = new JButton("Play");
playButton.addActionListener(handler);
c.add(playButton);
}


//////////////////////////////////////////////////////////////////////////
//
// Listen to the buttons.
//

//////////////////////////////////////////////////////////////////////////
private class ButtonHandler implements ActionListener {
public void actionPerformed (ActionEvent e) {
if (e.getSource() == playButton) {
sound = getAudioClip(getDocumentBase(), "Stone.wav");
sound.play();
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException x) {
System.out.println("whoops, can't get to sleep");
}
sound.stop();
System.out.println("Sound is stopped");
}
}
}
} // end of class

Ken Kalish

unread,
Jun 11, 2001, 12:52:12 AM6/11/01
to
On Mon, 11 Jun 2001 04:03:05 GMT, Eric Osman <os7...@mediaone.net> wrote:

>
>
>Why doesn't the following program work, according to your
>suggestion ?

Well, it works in an app under Sun 1.2.2

Maybe this has to do with the browser's jvm. You might try your code as an app
to see that it does release the sound card.

--

Eric Osman

unread,
Jun 11, 2001, 12:14:25 PM6/11/01
to

I'd love to try the program as an app instead of as an appLET
but I don't know how to make getDocumentBase happy.

Here's both the error stack I get and my source code:

java -classpath "." PlayAudio
Exception occurred during event dispatching:
java.lang.NullPointerException
at java.applet.Applet.getDocumentBase(Applet.java:95)
at PlayAudio$ButtonHandler.actionPerformed(PlayAudio.java:75)
at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:14
50)
at
javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Abstra
ctButton.java:1504)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:378)


//////////////////////////////////////////////////////////////////////////
//
// The following code used for running as an app instead of applet .
//

//////////////////////////////////////////////////////////////////////////
public static void main (String args[]) {
int width=300, height=200;
JFrame appWin = new JFrame("Sound demo");

appWin.addWindowListener
(
new WindowAdapter () {
public void windowClosing (WindowEvent e) {
System.exit(0);
}
}
);

PlayAudio applet = new PlayAudio();
applet.width = width;
applet.height = height;
applet.init();
applet.start();
appWin.getContentPane().add(applet);
appWin.setSize(width, height);
appWin.show();

Eric Osman

unread,
Jun 12, 2001, 11:59:47 AM6/12/01
to

The following program solves the problem, proven by the
following test:

1) Start the program (source code below!)
2) Play a sounds from win 98 sounds control panel to verify
that you can hear system sounds
3) Click "play" on the program to verify that you can hear
the program
4) Leave program running and play another sound from sounds
control panel to verify that you can still hear them

Thanks. /Eric

Here's source program (currently works as local app, but could
easily be modified to work as an applet, send me email
os7...@mediaone.net if you are interested) :

//////////////////////////////////////////////////////////////////////////
//
// Demo to play audio in java

//
// Creation: Eric Osman, os7...@mediaone.net , 6/8/01
//
//////////////////////////////////////////////////////////////////////////

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

import java.io.*;
import java.net.*;
import javax.sound.sampled.*;
import javax.sound.sampled.DataLine.*;
import javax.swing.*;

public class PlayAudio extends JApplet {

public int width=300;
public int height=200;

public JButton playButton;

AudioInputStream stream = null;
Clip clip = null;
Line.Info lineInfo = null;
AudioFormat audioFormat = null;

appWin.setLocation(200, 200);
appWin.show();
}


//////////////////////////////////////////////////////////////////////////
//
// Print diagnostic message.
//

//////////////////////////////////////////////////////////////////////////
static void print (String msg) {
System.out.println(msg);
}


//////////////////////////////////////////////////////////////////////////
//
// Listen to the buttons.
//

//////////////////////////////////////////////////////////////////////////
private class ButtonHandler implements ActionListener {
public void actionPerformed (ActionEvent e) {
if (e.getSource() == playButton) {

try {
stream = AudioSystem.getAudioInputStream
(new File("stone.wav"));
} catch (UnsupportedAudioFileException x) {
System.out.println("unsupported audio file");
} catch (IOException x) {
System.out.println("IO failure on audio file");
}
audioFormat = stream.getFormat();
lineInfo = new DataLine.Info(Clip.class, audioFormat);
try {
clip = (Clip) AudioSystem.getLine(lineInfo);
} catch (LineUnavailableException x) {
System.out.println("Failed to get line info");
}
try {
clip.open(stream);
} catch (LineUnavailableException x) {
System.out.println("Line unavailable to open clip");
} catch (IOException x) {
System.out.println("IO failure opening clip");
}
clip.loop(0);
try {
Thread.currentThread().sleep(2000);


} catch (InterruptedException x) {
System.out.println("whoops, can't get to sleep");
}

clip.close();
}
}
}
} // end of class

Ken Kalish

unread,
Jun 14, 2001, 12:01:58 AM6/14/01
to
Right, looking at the bug parade verifies that calling stop() on an AudioClip
works for 1.2 and previous, but is broken as of 1.3

javax.sound works, but is only introduced as of 1.3 - so there apparently is
no solution to work across all versions.

--

0 new messages