Solved

Sound for Java Application

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android development question 2 81
CSV file parsing thru Java 13 40
Java array 10 63
activeMQ Queue Messages Enqueued and Messages Dequeued 1 39
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.

726 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