Solved

Sound for Java Application

Posted on 2000-05-07
5
973 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now