PDA

View Full Version : Java Sound Playback




stadidas
Mar 21, 2006, 02:14 PM
Hi everyone on the forum.

I currently have this code in my "About Box" Java which allows for an image to display:

ImageIcon icon = createImageIcon("icon.gif", "The app icon.");
appIcon = new JLabel (icon);

I was wandering if it is possible to make it so that when the image is clicked on a brief aif sound clip is played, as a sort of easter egg. Is this possible? If so, does anyone know how to do it?
Ideally I would like it to play a random sound from an arraylist of sounds, but if this isn't possible then one sound will suffice.



stadidas
Mar 21, 2006, 07:39 PM
Any ideas anyone?

Dave the Great
Mar 21, 2006, 10:14 PM
Any ideas anyone?
Yes, it is possible to do.
Check out MouseEvent mouseClicked.

Yes, you can do random sounds.

coconn06
Mar 22, 2006, 02:12 PM
Take a look here (http://data.uta.edu/~telemm/JavaSound/SoundPlayer.java) for sample code on loading and playing a sound clip within your app (this example sets up it's own Swing application that loads files from a directory and allows you to select which one you want to play).

As for getting the clip to play when you click the image: I'm not sure if you can register a listener for an image exactly, so you might need to place the image within it's own JPanel and register a listener on the panel. Something like this (doing this from memory so forgive my errors - check the Java docs):


JPanel imagePanel = new JPanel();
/* Code to add the image to the panel goes here */
imagePanel.addMouseListener(this); // this class must implement the MouseListener interface
public void mouseClicked(MouseEvent e) {
soundPlayer.playRandomSound();
}


This code will set the current class as a MouseListener for your JPanel containing the image, so when the mouse is clicked it will call the mouseClicked method in your class.

stadidas
Mar 22, 2006, 06:52 PM
OK, I've made some progress. I now have:

ImageIcon icon = createImageIcon("icon.gif", "The app icon.");
appIcon = new JLabel (icon);

JPanel imagePanel = new JPanel ();
imagePanel.add(appIcon);
imagePanel.addMouseListener(new MouseAdapter () {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(frame, "Hello.");
}
});

I've used the option pane as a way to show that the MouseListener works before implementing the sound code. However, I get an error on the JOptionPane line stating "cannot resolve symbol". Anyone know what I'm doing wrong?

Dave the Great
Mar 22, 2006, 11:55 PM
OK, I've made some progress. I now have:

ImageIcon icon = createImageIcon("icon.gif", "The app icon.");
appIcon = new JLabel (icon);

JPanel imagePanel = new JPanel ();
imagePanel.add(appIcon);
imagePanel.addMouseListener(new MouseAdapter () {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(frame, "Hello.");
}
});

I've used the option pane as a way to show that the MouseListener works before implementing the sound code. However, I get an error on the JOptionPane line stating "cannot resolve symbol". Anyone know what I'm doing wrong?

You might want to check out your import statements and make sure you have defined 'frame'.

stadidas
Mar 23, 2006, 08:24 AM
ok, I defined the frame and now the code compiles, but no window pops up when it's clicked. :confused:

coconn06
Mar 23, 2006, 10:38 AM
Here's an app I wrote quickly which works, and shows the dialog box when the image is clicked. I hope this helps.

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

public class MyFrame extends JFrame {

public MyFrame() {
ImageIcon image = new ImageIcon("C:\\image.jpg");
JLabel label = new JLabel(image);

JPanel imagePanel = new JPanel ();
imagePanel.add(label);
imagePanel.addMouseListener(new MouseAdapter () {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null, "Hello.");
}
});

setTitle("Test JFrame");
Container content = getContentPane();
content.add(imagePanel);
this.pack();
setVisible(true);
}

public static void main(String[] args) {
MyFrame frame = new MyFrame();
}
}

stadidas
Mar 23, 2006, 03:13 PM
I now have this which works:

JPanel imagePanel = new JPanel ();
imagePanel.add(appIcon);
this.addMouseListener(new MouseAdapter () {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null, "Hello.");
}
});

Now, on to the sound! I'm going to use the AudioClip interface to achieve sound play back. Any problems I will let you know.

stadidas
Mar 23, 2006, 03:33 PM
Nearly there. I have imported sun.audio.*; and I've now got this code:

JPanel imagePanel = new JPanel ();
imagePanel.add(appIcon);
this.addMouseListener(new MouseAdapter () {
public void mouseClicked(MouseEvent e) {
InputStream in = new FileInputStream(../david.aif);
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);
JOptionPane.showMessageDialog(null, "Hello.");
}
});

I get an error on the InputStream line saying "illegal start of expression". I'm not quite sure how to fix this.
Also the "david.aif" file is in the root directory of the project, along with the java files, will the path I've used access this?

Any help appreciated.

gekko513
Mar 24, 2006, 01:46 AM
You forgot quotes around ../david.aif

stadidas
Mar 25, 2006, 03:35 PM
I've used the quotation marks around ../david.aif, but now I just get "cannot resolve symbol.

coconn06
Mar 28, 2006, 12:17 PM
Your code looks correct to me. Maybe there's a problem in some of the code outside of the snippet you provided. Other than that I'm not sure how to help =\

jeremy.king
Mar 28, 2006, 01:30 PM
I've used the quotation marks around ../david.aif, but now I just get "cannot resolve symbol.

Two things...

Post your entire class source.
Learn to use the Code tag, it will help with code readability.

My guess is you are missing imports or are referencing a variable that wasn't declared.