Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Sound Play Back in Java stand alone Application?  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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
  • 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);
}
});
}
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!