• 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

Sound Play Back in Java stand alone Application?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to play back audio sounds( *.au,*.midi,*.wav,) files from java stand alone applications?
get back me
------------------
S Arockiaraj
 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way I do this is set up the application as an appletcation, which is a hybrid of an application and an applet. You will also need to extend JApplet and use this AdapterClass shown below.
Below the AdapterClass here is a sample appletcation you can use as a guide. I do not know if this is the easiest way to do this, but I do know that it works. If anyone knows of an easier way I would like to know. Thanks!!!

Barry
import java.awt.*;
import java.applet.*;
import java.net.URL;
import java.util.Enumeration;
class AppletAdapter implements AppletContext,AppletStub
{
public URL getDocumentBase()
{
try
{
return new URL("file:" + System.getProperty("user.dir") +
System.getProperty("file.separator"));
}
catch (Exception e) { return null; }
}
public AudioClip getAudioClip(URL url)
{
try
{
return Applet.newAudioClip(url);
}
catch (Exception e) { return null; }
}
public Image getImage(URL url)
{
return Toolkit.getDefaultToolkit().getImage(url);
}
public URL getCodeBase() { return getDocumentBase(); }
public boolean isActive() { return true; }
public AppletContext getAppletContext() { return this; }
// Unimplemented methods -------------------------------
public String getParameter(String name) { return null; }
public Image getImage() { return null; }
public Applet getApplet(String name) { return null; }
public Enumeration getApplets() { return null; }
public void showDocument(URL url) { ; }
public void showDocument(URL url, String target) { ; }
public void showStatus(String status) { ; }
public void appletResize(int width, int height) { ; }
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class MediaAppletcation extends JApplet
{
Image theImage;
AudioClip theClip;
public void init()
{
Container c = getContentPane();
theClip = getAudioClip(getDocumentBase(), "Music.au");
theImage = getImage(getDocumentBase(), "pizza.jpg");
}
public void paint(Graphics g)
{
int w = getSize().width -
(getInsets().left + getInsets().right);
int h = getSize().height -
(getInsets().top + getInsets().bottom);
super.paint(g);
if (theImage == null | |
!g.drawImage( theImage,
getInsets().left, getInsets().top,
w, h, this))
repaint(300);
}
public void start() { theClip.loop(); }
public void stop() { theClip.stop(); }
public static void main(String [] args)
{
MediaAppletcation applet = new MediaAppletcation();
applet.setStub(new AppletAdapter());
JFrame theFrame =
applet.new MainFrame("The MediaAppletcation");
theFrame.getContentPane().add(applet, "Center");
theFrame.show();
applet.init();
applet.start();
applet.invalidate();
applet.validate();
}
class MainFrame extends JFrame
{
public MainFrame(String title)
{
super(title);
setSize(500, 400);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
}
}
 
Oh, sure, you could do that. Or you could eat some pie. While reading this 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