Solved

Sound for Java Application

Posted on 2000-05-07
5
991 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

622 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