Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sound for Java Application

Posted on 2000-05-07
5
Medium Priority
?
995 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 200 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
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:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

715 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