Solved

Sound for Java Application

Posted on 2000-05-07
5
980 Views
Last Modified: 2013-11-23
How to insert method about sound to java
application? Now I have application program and I want to insert sound in my program when I click botton . I want to know about what method and how can use it. plese tell about detail for me.

** I use jdk 1.3 **
0
Comment
Question by:Arsari
  • 3
5 Comments
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2787382
From: pagladasu
 Date: Friday, March 31 2000 - 02:22AM CST  
Try something like this:
import sun.audio.*;
import java.io.*;
public class audio{
  public static void main(String args[]){
    AudioPlayer player=AudioPlayer.player;
       try{
           AudioStream audiostr =
              new AudioStream(new  FileInputStream("myaudio.au"));
player.start(audiostr);
       }
       catch(IOException err){
System.out.println(err.toString());
       }
  }
}

0
 
LVL 3

Expert Comment

by:rjackman
ID: 2787447
if u are using the applet the u can use interface AudioClip
import java.applet.*;
public class Audio extends Applet
{
Private AudioClip audio;  
private URL url;
 public void init()
{
url = this.getCodeBase("name of the clip");
  audio = this.getAudioClip(url,audioname); //audioname :name of the
audio clip
}
}
0
 
LVL 3

Expert Comment

by:rjackman
ID: 2787458
oh i forgot 1 thing
in paint method where u are displaying image u have to insert following code
if(audio!=null)
{
  audio.play();
}
if u are not using image and paint method then u have to call it from some where else
RJackman
0
 
LVL 7

Accepted Solution

by:
Sasha_Mapa earned 50 total points
ID: 2787716
rjackman? playing a clip from the paint method? that's not nice!!



Ok, here's how you do it from an applet:

AudioClip clip = getAudioClip(getCodeBase(),"music.wav");
clip.play();

or just
play(getCodeBase(),"music.wav");

just make sure there's a music.wav file in the directory you are running your applet from.

To do that from an application, you just have to use a different method (which you can also use for an applet btw, but it's less convenient) to get the clip.

AudioClip clip = null;
try{
  clip = Applet.newAudioClip(new File("music.wav")).toURL());
} catch (MalformedURLException e){
   e.printStackTrace();
   }
// and now play it just the same way:
clip.play();


you can also invoke clip.stop() to stop it when its playing and clip.loop() to have it play again when it finishes infinitely until you invoke clip.stop()...

Here is an example that does exactly what you asked for:

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



public class MyApp extends WindowAdapter implements ActionListener{

   static AudioClip clip;
                       
   public static void main(String[] args ){
       Frame f = new Frame();
       f.reshape(50,50,500,500);
       f.addWindowListener(new MyApp());
       f.setLayout(null);
       f.show();
       Button b = new Button("Click me");
       b.addActionListener(new MyApp());
       b.reshape(50,50,60,20);
       f.add(b);
                         
       try{
         clip = Applet.newAudioClip(   (new File("Gong.wav")).toURL()   );
       } catch (MalformedURLException e){
             e.printStackTrace();
          }
    }

    public void windowClosing(WindowEvent evt){
       evt.getWindow().dispose();
       System.exit(0);
    }

    public void actionPerformed(ActionEvent evt){
       if (clip == null)
          System.out.println("clip is null");
       else{
          System.out.println("playing clip");
          clip.play();
       }
                             
   }

}

                      Good luck, Sasha.
0
 
LVL 3

Expert Comment

by:rjackman
ID: 2791383
Sasha thanx  for the tip...
:):)rickyjackman
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows 10 IE Certificate Issue 10 45
hibernate jars 4 32
Facing this issue for maven proxy setting 2 12
Glassfish admin console not working 1 13
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question