• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

running audio in an applet AND an application

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can run this as an applet but not as an application. Can someone help make this program do either or?
import java.applet.*;
import java.awt.BorderLayout;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import Chapter8.MyFrameWithExitHandling;
public class Lab14_1 extends JApplet implements ActionListener
{
// Delcare Audio Files
static AudioClip playAudio;
protected AudioClip loopAudio;
protected AudioClip stopAudio;
// Declare Buttons
private JButton jplay = new JButton("PLAY");
private JButton jloop = new JButton("LOOP");
private JButton jstop = new JButton("STOP");
private boolean isStandalone = false;
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();
applet.isStandalone = true;
// Add the applet instance to the frame
frame.getContentPane().add(applet, BorderLayout.CENTER);
// Invoke init() and start()
applet.init();
applet.start();
// Display the frame
frame.pack();
frame.center();
frame.setVisible(true);
}
public void init()
{
if (!isStandalone)
{
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.add(jplay);
p.add(jloop);
p.add(jstop);
getContentPane().add(p);
// Create audio clips for playAudio, loopAudio
playAudio = getAudioClip(getCodeBase(),
"Chapter14/Airplane.wav");
loopAudio = getAudioClip(getCodeBase(),
"Chapter14/Chimp.wav");
// Register Listeners
jplay.addActionListener(this);
jloop.addActionListener(this);
jstop.addActionListener(this);
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jplay)
{
playAudio.play();
}
if (e.getSource() == jloop)
{
loopAudio.loop();
}
if (e.getSource() == jstop)
{
playAudio.stop();
loopAudio.stop();
}
}
}
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two things, one thing is in your init method, when you want it to run as stand alone program, you should say if (standalone) instead of if (!standalone).
The other thing is when you use it as an applicaiton, it won't get the code base for the applet since it doesn't have an applet context supplied, so maybe you should try to just passing the file url to the audio in the format like new URL("file:///c:/directory/1.wav").
Hope this helps.

Originally posted by Andrew Hoffman:
I can run this as an applet but not as an application. Can someone help make this program do either or?
import java.applet.*;
import java.awt.BorderLayout;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import Chapter8.MyFrameWithExitHandling;
public class Lab14_1 extends JApplet implements ActionListener
{
// Delcare Audio Files
static AudioClip playAudio;
protected AudioClip loopAudio;
protected AudioClip stopAudio;
// Declare Buttons
private JButton jplay = new JButton("PLAY");
private JButton jloop = new JButton("LOOP");
private JButton jstop = new JButton("STOP");
private boolean isStandalone = false;
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();
applet.isStandalone = true;
// Add the applet instance to the frame
frame.getContentPane().add(applet, BorderLayout.CENTER);
// Invoke init() and start()
applet.init();
applet.start();
// Display the frame
frame.pack();
frame.center();
frame.setVisible(true);
}
public void init()
{
if (!isStandalone)
{
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.add(jplay);
p.add(jloop);
p.add(jstop);
getContentPane().add(p);
// Create audio clips for playAudio, loopAudio
playAudio = getAudioClip(getCodeBase(),
"Chapter14/Airplane.wav");
loopAudio = getAudioClip(getCodeBase(),
"Chapter14/Chimp.wav");
// Register Listeners
jplay.addActionListener(this);
jloop.addActionListener(this);
jstop.addActionListener(this);
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jplay)
{
playAudio.play();
}
if (e.getSource() == jloop)
{
loopAudio.loop();
}
if (e.getSource() == jstop)
{
playAudio.stop();
loopAudio.stop();
}
}
}


 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic