Solved

How do I add a sound effect ???

Posted on 2004-10-03
12
244 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.

920 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

15 Experts available now in Live!

Get 1:1 Help Now