Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 21, 2006, 02:14 PM   #1
stadidas
macrumors regular
 
Join Date: Feb 2006
Location: Kent, United Kingdom
Java Sound Playback

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 is offline   0 Reply With Quote
Old Mar 21, 2006, 07:39 PM   #2
stadidas
Thread Starter
macrumors regular
 
Join Date: Feb 2006
Location: Kent, United Kingdom
Any ideas anyone?
stadidas is offline   0 Reply With Quote
Old Mar 21, 2006, 10:14 PM   #3
Dave the Great
Banned
 
Join Date: Jan 2004
Quote:
Originally Posted by stadidas
Any ideas anyone?
Yes, it is possible to do.
Check out MouseEvent mouseClicked.

Yes, you can do random sounds.
Dave the Great is offline   0 Reply With Quote
Old Mar 22, 2006, 02:12 PM   #4
coconn06
macrumors regular
 
Join Date: Jun 2003
Location: King of Prussia, PA
Send a message via AIM to coconn06
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.
__________________
-Colin
24" iMac 2.8 GHz C2D
iPhone 3G 8GB (soon to be iPhone 4 32GB)
coconn06 is offline   0 Reply With Quote
Old Mar 22, 2006, 06:52 PM   #5
stadidas
Thread Starter
macrumors regular
 
Join Date: Feb 2006
Location: Kent, United Kingdom
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?
stadidas is offline   0 Reply With Quote
Old Mar 22, 2006, 11:55 PM   #6
Dave the Great
Banned
 
Join Date: Jan 2004
Quote:
Originally Posted by stadidas
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'.
Dave the Great is offline   0 Reply With Quote
Old Mar 23, 2006, 08:24 AM   #7
stadidas
Thread Starter
macrumors regular
 
Join Date: Feb 2006
Location: Kent, United Kingdom
ok, I defined the frame and now the code compiles, but no window pops up when it's clicked.
stadidas is offline   0 Reply With Quote
Old Mar 23, 2006, 10:38 AM   #8
coconn06
macrumors regular
 
Join Date: Jun 2003
Location: King of Prussia, PA
Send a message via AIM to coconn06
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();
    }
}
__________________
-Colin
24" iMac 2.8 GHz C2D
iPhone 3G 8GB (soon to be iPhone 4 32GB)
coconn06 is offline   0 Reply With Quote
Old Mar 23, 2006, 03:13 PM   #9
stadidas
Thread Starter
macrumors regular
 
Join Date: Feb 2006
Location: Kent, United Kingdom
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 is offline   0 Reply With Quote
Old Mar 23, 2006, 03:33 PM   #10
stadidas
Thread Starter
macrumors regular
 
Join Date: Feb 2006
Location: Kent, United Kingdom
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.
stadidas is offline   0 Reply With Quote
Old Mar 24, 2006, 01:46 AM   #11
gekko513
macrumors 601
 
gekko513's Avatar
 
Join Date: Oct 2003
You forgot quotes around ../david.aif
gekko513 is offline   0 Reply With Quote
Old Mar 25, 2006, 03:35 PM   #12
stadidas
Thread Starter
macrumors regular
 
Join Date: Feb 2006
Location: Kent, United Kingdom
I've used the quotation marks around ../david.aif, but now I just get "cannot resolve symbol.
stadidas is offline   0 Reply With Quote
Old Mar 28, 2006, 12:17 PM   #13
coconn06
macrumors regular
 
Join Date: Jun 2003
Location: King of Prussia, PA
Send a message via AIM to coconn06
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 =\
__________________
-Colin
24" iMac 2.8 GHz C2D
iPhone 3G 8GB (soon to be iPhone 4 32GB)
coconn06 is offline   0 Reply With Quote
Old Mar 28, 2006, 01:30 PM   #14
jeremy.king
macrumors 603
 
jeremy.king's Avatar
 
Join Date: Jul 2002
Location: Fox Lake, IL
Quote:
Originally Posted by stadidas
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.
jeremy.king is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Itunes 11.1 no sound in full screen playback Curtispro Mac Applications and Mac App Store 12 Oct 25, 2013 02:49 PM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 09:13 AM
iTunes choppy audio playback & no video playback Arkious Mac Applications and Mac App Store 2 May 23, 2013 06:29 AM
Stuttering during playback of video/sound, kernel panic rreimche Mac OS X 10.7 Lion 12 Mar 30, 2013 10:23 AM
Sound delay when playback starts on Apple TV 3 zen Apple TV and Home Theater 1 Dec 4, 2012 05:04 AM

Forum Jump

All times are GMT -5. The time now is 05:30 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC