Solved

How do I add a sound effect ???

Posted on 2004-10-03
12
245 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
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12210807
0
 
LVL 92

Expert Comment

by:objects
ID: 12210817
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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 250 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 150 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 77
web services creation SOAP vs REST 5 37
oracle 11g 23 73
Groovy problem when using SOAPUI : DispatchException occurred 7 43
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 …
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:

813 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

11 Experts available now in Live!

Get 1:1 Help Now