Total members 11893 |It is currently Mon Nov 04, 2024 11:09 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





play a sound when the user enters the area- audio feedback
java code
/*
* Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
/*
* @(#)SoundArea.java 1.3 95/10/13
*
* Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
* without fee is hereby granted.
* Please refer to the file http://java.sun.com/copy_trademarks.html
* for further important copyright and trademark information and to
* http://java.sun.com/licensing.html for further important licensing
* information for the Java (tm) Technology.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*
* THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
* CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
* PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
* NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
* SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
* SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
* PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). SUN
* SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
* HIGH RISK ACTIVITIES.
*/

import java.awt.Graphics;
import java.applet.AudioClip;
import java.net.URL;
import java.net.MalformedURLException;

/**
* An audio feedback ImageArea class.
* This class extends the basic ImageArea Class to play a sound when
* the user enters the area.
*
* @author Jim Graham
* @author Chuck McManis
* @version 1.3, 13 Oct 1995
*/
class SoundArea extends ImageMapArea {
/** The URL of the sound to be played. */
URL sound;
AudioClip soundData = null;
boolean hasPlayed;
boolean isReady = false;
long lastExit = 0;
final static int HYSTERESIS = 1500;

/**
* The argument is the URL of the sound to be played.
*/
public void handleArg(String arg) {
try {
sound = new URL(parent.getDocumentBase(), arg);
} catch (MalformedURLException e) {
sound = null;
}
hasPlayed = false;
}

/**
* The applet thread calls the getMedia() method when the applet
* is started.
*/
public void getMedia() {
if (sound != null) {
soundData = parent.getAudioClip(sound);
}
if (soundData == null) {
System.out.println("SoundArea: Unable to load data "+sound);
}
isReady = true;
}

/**
* The enter method is called when the mouse enters the area.
* The sound is played if the mouse has been outside of the
* area for more then the delay indicated by HYSTERESIS.
*/
public boolean enter() {
// is the sound sample loaded?
if (! isReady) {
parent.showStatus("Loading media file...");
return false;
}

/*
* So we entered the selection region, play the sound if
* we need to. Track the mouse entering and exiting the
* the selection box. If it doesn't stay out for more than
* "HYSTERESIS" millis, then don't re-play the sound.
*/
long now = System.currentTimeMillis();
if (Math.abs(now - lastExit) < HYSTERESIS) {
// if within the window pretend that it was played.
hasPlayed = true;
return false;
}

// Else play the sound.
if (! hasPlayed && (soundData != null)) {
hasPlayed = true;
soundData.play();
}

return false;
}

/**
* The exit method is called when the mouse leaves the area.
*/
public void exit() {
if (hasPlayed) {
hasPlayed = false;
lastExit = System.currentTimeMillis(); // note the time of exit
}
}
}


java code
/*
* Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
/*
* @(#)ImageMapArea.java 1.3 95/10/13
*
* Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
* without fee is hereby granted.
* Please refer to the file http://java.sun.com/copy_trademarks.html
* for further important copyright and trademark information and to
* http://java.sun.com/licensing.html for further important licensing
* information for the Java (tm) Technology.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*
* THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
* CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
* PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
* NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
* SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
* SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
* PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). SUN
* SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
* HIGH RISK ACTIVITIES.
*/

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.*;
import java.util.StringTokenizer;
import java.net.URL;
import java.net.MalformedURLException;

/**
* The base ImageArea class.
* This class performs the basic functions that most ImageArea
* classes will need and delegates specific actions to the subclasses.
*
* @author Jim Graham
* @version 1.3, 13 Oct 1995
*/
class ImageMapArea implements ImageObserver {
/** The applet parent that contains this ImageArea. */
ImageMap parent;
/** The X location of the area (if rectangular). */
int X;
/** The Y location of the area (if rectangular). */
int Y;
/** The size().width of the area (if rectangular). */
int W;
/** The size().height of the area (if rectangular). */
int H;
/**
* This flag indicates whether the user was in this area during the
* last scan of mouse locations.
*/
boolean entered = false;
/** This flag indicates whether the area is currently highlighted. */
boolean active = false;

/**
* This is the default highlight image if no special effects are
* needed to draw the highlighted image. It is created by the
* default "makeImages()" method.
*/
Image hlImage;

/**
* Initialize this ImageArea as called from the applet.
* If the subclass does not override this initializer, then it
* will perform the basic functions of setting the parent applet
* and parsing out 4 numbers from the argument string which specify
* a rectangular region for the ImageArea to act on.
* The remainder of the argument string is passed to the handleArg()
* method for more specific handling by the subclass.
*/
public void init(ImageMap parent, String args) {
this.parent = parent;
StringTokenizer st = new StringTokenizer(args, ", ");
X = Integer.parseInt(st.nextToken());
Y = Integer.parseInt(st.nextToken());
W = Integer.parseInt(st.nextToken());
H = Integer.parseInt(st.nextToken());
if (st.hasMoreTokens()) {
// hasMoreTokens() Skips the trailing comma
handleArg(st.nextToken(""));
} else {
handleArg(null);
}
makeImages();
}

/**
* This method handles the remainder of the argument string after
* the standard initializer has parsed off the 4 rectangular
* parameters. If the subclass does not override this method,
* the remainder will be ignored.
*/
public void handleArg(String s) {
}

/**
* This method loads any additional media that the ImageMapArea
* may need for its animations.
*/
public void getMedia() {
}

/**
* This method is called every animation cycle if there are any
* active animating areas.
* @return true if this area requires further animation notifications
*/
public boolean animate() {
return false;
}

/**
* This method sets the image to be used to render the ImageArea
* when it is highlighted.
*/
public void setHighlight(Image img) {
hlImage = img;
}

/**
* This method handles the construction of the various images
* used to highlight this particular ImageArea when the user
* interacts with it.
*/
public void makeImages() {
setHighlight(parent.getHighlight(X, Y, W, H));
}

/**
* The repaint method causes the area to be repainted at the next
* opportunity.
*/
public void repaint() {
parent.repaint(0, X, Y, W, H);
}

/**
* This method tests to see if a point is inside this ImageArea.
* The standard method assumes a rectangular area as parsed by
* the standard initializer. If a more complex area is required
* then this method will have to be overridden by the subclass.
*/
public boolean inside(int x, int y) {
return (x >= X && x < (X + W) && y >= Y && y < (Y + H));
}

/**
* This utility method draws a rectangular subset of a highlight
* image.
*/
public void drawImage(Graphics g, Image img, int imgx, int imgy,
int x, int y, int w, int h) {
Graphics ng = g.create();
ng.clipRect(x, y, w, h);
ng.drawImage(img, imgx, imgy, this);
}

/**
* This method handles the updates from drawing the images.
*/
public boolean imageUpdate(Image img, int infoflags,
int x, int y, int width, int height) {
if (img == hlImage) {
return parent.imageUpdate(img, infoflags, x + X, y + Y,
width, height);
} else {
return (infoflags & (ALLBITS | ERROR)) == 0;
}
}

/**
* This utility method shows a string in the status bar.
*/
public void showStatus(String msg) {
parent.getAppletContext().showStatus(msg);
}

/**
* This utility method tells the browser to visit a URL.
*/
public void showDocument(URL u) {
parent.getAppletContext().showDocument(u);
}

/**
* This method highlights the specified area when the user enters
* it with his mouse. The standard highlight method is to replace
* the indicated rectangular area of the image with the primary
* highlighted image.
*/
public void highlight(Graphics g) {
}

/**
* The checkEnter method is called when the mouse is inside the
* region to see if the area needs to have its enter method called.
* The default implementation simply checks if the entered flag is
* set and only calls enter if it is false.
*/
public boolean checkEnter(int x, int y) {
if (entered) {
return isTerminal();
} else {
entered = true;
return enter(x, y);
}
}

/**
* The checkExit method is called when the mouse is outside the
* region to see if the area needs to have its exit method called.
* The default implementation simply checks if the entered flag is
* set and only calls exit if it is true.
*/
public void checkExit() {
if (entered) {
entered = false;
exit();
}
}

/**
* The isTerminal method controls whether events propagate to the
* areas which lie beneath this one.
* @return true if the events should be propagated to the underlying
* areas.
*/
public boolean isTerminal() {
return false;
}

/**
* The enter method is called when the mouse enters the region.
* The location is supplied, but the standard implementation is
* to call the overloaded method with no arguments.
* @return true if this ImageMapArea wants to prevent any underlying
* areas from seeing the enter
*/
public boolean enter(int x, int y) {
return enter();
}

/**
* The overloaded enter method is called when the mouse enters
* the region. This method can be overridden if the ImageArea
* does not need to know where the mouse entered.
* @return true if this ImageMapArea wants to prevent any underlying
* areas from seeing the enter
*/
public boolean enter() {
return isTerminal();
}

/**
* The exit method is called when the mouse leaves the region.
*/
public void exit() {
}

/**
* The press method is called when the user presses the mouse
* button inside the ImageArea. The location is supplied, but
* the standard implementation is to call the overloaded method
* with no arguments.
* @return true if this ImageMapArea wants to prevent any underlying
* areas from seeing the press
*/
public boolean press(int x, int y) {
return press();
}

/**
* The overloaded press method is called when the user presses the
* mouse button inside the ImageArea. This method can be overridden
* if the ImageArea does not need to know the location of the press.
* @return true if this ImageMapArea wants to prevent any underlying
* areas from seeing the press
*/
public boolean press() {
return isTerminal();
}

/**
* The lift method is called when the user releases the mouse button.
* The location is supplied, but the standard implementation is to
* call the overloaded method with no arguments. Only those ImageAreas
* that were informed of a press will be informed of the corresponding
* release.
* @return true if this ImageMapArea wants to prevent any underlying
* areas from seeing the lift
*/
public boolean lift(int x, int y) {
return lift();
}

/**
* The overloaded lift method is called when the user releases the
* mouse button. This method can be overridden if the ImageArea
* does not need to know the location of the release.
* @return true if this ImageMapArea wants to prevent any underlying
* areas from seeing the lift
*/
public boolean lift() {
return isTerminal();
}

/**
* The drag method is called when the user moves the mouse while
* the button is pressed. Only those ImageAreas that were informed
* of a press will be informed of the corresponding mouse movements.
* @return true if this ImageMapArea wants to prevent any underlying
* areas from seeing the drag
*/
public boolean drag(int x, int y) {
return isTerminal();
}
}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : play a sound when the user enters the area- audio feedback
 user feedback FORM ASP     -  
 Sound Player which save Mic sound     -  
 Java- Copy text area into disabled text area     -  
 play music as your website background.     -  
 Feedback Form in php     -  
 FeedBack form Spam     -  
 add sound to your animations in flash     -  
 Sound Level Meter     -  
 Area clipping     -  
 compute area of the circle.     -  



Topic Tags

Java Sound






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com