Solved

How do I add a sound effect ???

Posted on 2004-10-03
12
247 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

687 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