?
Solved

How do I add a sound effect ???

Posted on 2004-10-03
12
Medium Priority
?
248 Views
Last Modified: 2010-03-31
Hi Experts,

   How do I add a sound effect in a main frame after certain things happen ? thanks !
0
Comment
Question by:meow00
[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
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12210807
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12210831
Be careful meow00 -if you want to play standard sound files you usually have to use the full sound API
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:meow00
ID: 12211537
err ... then can I play sound in a simple frame :
(I only need very simple sound, such as "ding". Is there any default files in the java package ?)

i.e. what would be the easiest way to just put a "ding" in the following place ??? many thanks !

------------------------------------------------------------------
import javax.swing.*;
import java.awt.* ;
import java.awt.event.* ;

public class MyTest extends JFrame{
    private int i ;  
    public MyTest() {
   
        int i=0 ;
        for(i=0; i<100; i++){
          if(i%10==0) {
           // I want to play a simple sound here !!!
       
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        setSize(600,600) ;
        show() ;
       
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        MyTest meow = new MyTest() ;
        // TODO code application logic here
    }
   
 }

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12211630
Search your system for .au files
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12211641
Or you might be lucky with something like:

for(int i=0; i<100; i++) {
      if (i % 10 == 0) {
            Toolkit.getDefaultToolkit().beep();
      }
}      

0
 
LVL 92

Expert Comment

by:objects
ID: 12213824
File file = new File("bark.wav");
AudioClip clip = Applet.newAudioClip(file.toURL());
clip.play();

0
 
LVL 15

Expert Comment

by:Javatm
ID: 12214044
Here is a sweet demo :

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

public class MyTest extends JFrame {

    private JButton b1;

    public MyTest() {
   
    b1 = new JButton(" Click To Play Sound ");
    this.getContentPane().add(b1, BorderLayout.CENTER);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    setSize(300,300) ;
    setVisible(true) ;        

    b1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent h)
      {
        File file = new File("C:/YourFolder/YourSound.wav");
        AudioClip clip = Applet.newAudioClip(file.toURL());
        clip.play();
      }
     });
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        MyTest meow = new MyTest() ;
        // TODO code application logic here
    }
 }
0
 
LVL 1

Author Comment

by:meow00
ID: 12214599
Hi objects & JavaTM,

  how come I got an error :

 Test.java [227:1] unreported exception java.net.MalformedURLException; must be caught or declared to be  thrown
                   AudioClip clip = Applet.newAudioClip(file.toURL());
------------------------
 I have imported the following :
 did I miss anything ?

 import java.io.* ;
import javax.swing.*;
import java.awt.* ;
import java.awt.event.* ;
import java.applet.*;
import java.net.URL;
0
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 12214615
you need to wrap it in a try/catch block:

try
{
   File file = new File("bark.wav");
   AudioClip clip = Applet.newAudioClip(file.toURL());
   clip.play();
}
catch (Exception ex)
{
   ex.printStackTrace();
}
0
 
LVL 1

Author Comment

by:meow00
ID: 12214638
Hi  Objects & JavaTm ....

   The code was compiled and ran ... but I didn't hear any sound ..... is there anything that I might have missed ? thanks !!!

Hi CHEJ,
 
   Do I need to import anything to use beep() ... as I heard nothing either :-(
0
 
LVL 15

Assisted Solution

by:Javatm
Javatm earned 600 total points
ID: 12214697
Try this :

-Reminder !, dont forget the location of the file !, wav, au and midi will work but not mp3.

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

public class MyTest extends JFrame {

    private JButton b1;

    public MyTest() {
   
    b1 = new JButton(" Click To Play Sound ");
    this.getContentPane().add(b1, BorderLayout.CENTER);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    setSize(300,300) ;
    setVisible(true) ;        

    b1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent h)
      {
        try {
        File file = new File("C:/YourFolder/YourSound.wav");
        AudioClip clip = Applet.newAudioClip(file.toURL());
        clip.play();
        }
        catch(Exception e) {
        e.printStackTrace();
        }
      }
     });
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        MyTest meow = new MyTest() ;
        // TODO code application logic here
    }
 }
0

Featured Post

Technology Partners: 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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

741 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