Java Sound Playback

Discussion in 'Mac Programming' started by stadidas, Mar 21, 2006.

  1. macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #1
    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.
     
  2. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
  3. macrumors regular

    Joined:
    Jan 27, 2004
    #3
    Yes, it is possible to do.
    Check out MouseEvent mouseClicked.

    Yes, you can do random sounds.
     
  4. macrumors regular

    Joined:
    Jun 14, 2003
    Location:
    King of Prussia, PA
    #4
    Take a look here 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):

    Code:
    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.
     
  5. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #5
    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?
     
  6. macrumors regular

    Joined:
    Jan 27, 2004
    #6
    You might want to check out your import statements and make sure you have defined 'frame'.
     
  7. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #7
    ok, I defined the frame and now the code compiles, but no window pops up when it's clicked. :confused:
     
  8. macrumors regular

    Joined:
    Jun 14, 2003
    Location:
    King of Prussia, PA
    #8
    Here's an app I wrote quickly which works, and shows the dialog box when the image is clicked. I hope this helps.

    Code:
    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();
        }
    }
     
  9. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #9
    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.
     
  10. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #10
    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.
     
  11. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
  12. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #12
    I've used the quotation marks around ../david.aif, but now I just get "cannot resolve symbol.
     
  13. macrumors regular

    Joined:
    Jun 14, 2003
    Location:
    King of Prussia, PA
    #13
    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 =\
     
  14. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #14
    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.
     

Share This Page