Solved

How do I add a sound effect ???

Posted on 2004-10-03
12
243 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

758 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

21 Experts available now in Live!

Get 1:1 Help Now