It's not a secret anymore!*
The moose likes Java in General and the fly likes PLEASE HELP QUICKLY!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "PLEASE HELP QUICKLY!!" Watch "PLEASE HELP QUICKLY!!" New topic
Author

PLEASE HELP QUICKLY!!

Andrew Hoffman
Greenhorn

Joined: Apr 30, 2001
Posts: 4
Sorry for the frantic subject line, but i have two short hours left before I am flushed down the perverbial toilet.
This applet plays a few audio clips. I need it to run as an applicaton, too. I don't know how to do that. Can someone be my hero and adjust the code acordingly? Thank You.
Andrew
***
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import Chapter8.MyFrameWithExitHandling;
import java.net.URL;
import java.applet.*;
public class Lab14_1 extends JApplet implements ActionListener
{
// Delcare Audio Files
protected AudioClip playAudio;
protected AudioClip loopAudio;
protected AudioClip stopAudio;
// Declare Buttons
private static JButton jplay = new JButton("PLAY");
private static JButton jloop = new JButton("LOOP");
private static JButton jstop = new JButton("STOP");
public void init()
{
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.add(jplay);
p.add(jloop);
p.add(jstop);
getContentPane().add(p);
// Register Listeners
jplay.addActionListener(this);
jloop.addActionListener(this);
jstop.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
// Get the file name
String filename = null;
if (e.getSource() == jplay)
filename = "Chapter14/Chimp.wav";
else if (e.getSource() == jloop)
filename = "Chaper14/Chimp.wav";
if
{
playAudio.stop();
loopAudio.loop();
}
if (e.getSource() == jstop)
{
playAudio.stop();
loopAudio.stop();
}
createClip(filename).play();
}
public AudioClip createClip(String filename)
{
// Get the URL for the file name
URL url = this.getClass().getResource("Chimp/" + filename);
// Return the audio clip
return Applet.newAudioClip(url);
}
public static void main(String [] args)
{
// Create a frame
MyFrameWithExitHandling frame = new MyFrameWithExitHandling(
"Lab14_1 Application");
// Crate an instance of the applet
Lab14_1 applet = new Lab14_1();
// Add applet to the fray
frame.getContentPane().add(applet, BorderLayout.CENTER);
// Invoke init() and start()
applet.init();
applet.start();
// Display the frame
frame.pack();
frame.center();
frame.setVisible(true);
}
}
pilania
Greenhorn

Joined: Mar 20, 2000
Posts: 26
This might help. This is the code for playing audio from Application and not from Applet.
import java.applet.AudioClip;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.GridBagLayout;
public class SoundApplication extends JPanel
implements ActionListener,
ItemListener {
SoundList soundList;
String auFile = "spacemusic.au";
String aiffFile = "flute+hrn+mrmba.aif";
String midiFile = "trippygaia1.mid";
String rmfFile = "jungle.rmf";
String wavFile = "bottle-open.wav";
String chosenFile;

AudioClip onceClip, loopClip;
URL codeBase;
JComboBox formats;
JButton playButton, loopButton, stopButton;
JLabel status;

boolean looping = false;
public SoundApplication() {
String [] fileTypes = {auFile,
aiffFile,
midiFile,
rmfFile,
wavFile};
formats = new JComboBox(fileTypes);
formats.setSelectedIndex(0);
chosenFile = (String)formats.getSelectedItem();
formats.addItemListener(this);
playButton = new JButton("Play");
playButton.addActionListener(this);
loopButton = new JButton("Loop");
loopButton.addActionListener(this);
stopButton = new JButton("Stop");
stopButton.addActionListener(this);
stopButton.setEnabled(false);

status = new JLabel(
"Click Play or Loop to play the selected sound file.");

JPanel controlPanel = new JPanel();
controlPanel.add(formats);
controlPanel.add(playButton);
controlPanel.add(loopButton);
controlPanel.add(stopButton);

JPanel statusPanel = new JPanel();
statusPanel.add(status);

add(controlPanel);
add(statusPanel);
startLoadingSounds();
}
public void itemStateChanged(ItemEvent e){
chosenFile = (String)formats.getSelectedItem();
soundList.startLoading(chosenFile);
}
void startLoadingSounds() {
//Start asynchronous sound loading.
try {
codeBase = new URL("file:" + System.getProperty("user.dir") + "/");
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
}
soundList = new SoundList(codeBase);
soundList.startLoading(auFile);
soundList.startLoading(aiffFile);
soundList.startLoading(midiFile);
soundList.startLoading(rmfFile);
soundList.startLoading(wavFile);
}
public void stop() {
onceClip.stop(); //Cut short the one-time sound.
if (looping) {
loopClip.stop(); //Stop the sound loop.
}
}
public void start() {
if (looping) {
loopClip.loop(); //Restart the sound loop.
}
}
public void actionPerformed(ActionEvent event) {
//PLAY BUTTON
Object source = event.getSource();
if (source == playButton) {
//Try to get the AudioClip.
onceClip = soundList.getClip(chosenFile);
stopButton.setEnabled(true);
onceClip.play(); //Play it once.
status.setText("Playing sound " + chosenFile + ".");
if (onceClip == null) {
status.setText("Sound " + chosenFile + " not loaded yet.");
}
return;
}
//START LOOP BUTTON
if (source == loopButton) {
loopClip = soundList.getClip(chosenFile);
looping = true;
loopClip.loop(); //Start the sound loop.
loopButton.setEnabled(false); //Disable start button.
stopButton.setEnabled(true);
status.setText("Playing sound " + chosenFile + " continuously.");
if (loopClip == null) {
status.setText("Sound " + chosenFile + " not loaded yet.");
}
return;
}
//STOP LOOP BUTTON
if (source == stopButton) {
if (looping) {
looping = false;
loopClip.stop(); //Stop the sound loop.
loopButton.setEnabled(true); //Enable start button.
} else if (onceClip != null) {
onceClip.stop();
}
stopButton.setEnabled(false);
status.setText("Stopped playing " + chosenFile + ".");
return;
}
}
public static void main(String s[]) {
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
};
JFrame f = new JFrame("SoundApplication");
f.addWindowListener(l);
f.getContentPane().add(new SoundApplication());
f.setSize(new Dimension(400,100));
f.show();
}
}
Did not have time to convert your program to this specs. Please note that your program is far simpler than this.
hope this helps!!

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: PLEASE HELP QUICKLY!!
 
Similar Threads
Running a program as an application and an applet
running audio in an applet AND an application
Java Help non-HTML textpad
IN NEED FAST HELP!!
help~!!!