简体   繁体   中英

In java, making a selective beep sound

I am trying to make several rectangles show up when the applet is started with a couple colors added in...I already have the beep audio file but from what i know, it will play throughout the whole program. Can someone please tell me how to make the beep sound occur only when the rectangles are clicked?

There is some simple Audio API directly in Applet that you can use like this:

public class MyApplet extends Applet
{
    protected AudioClip beep;
    protected relativeBeepPath = "mybeep.wav";

    public void init()
    {
        //....
        try {
            beep = getAudioClip( new URL( getDocumentBase(), relativeBeepPath ) );
        } catch( Exception ex ) {
            ex.printStackTrace();
        }
    }

    public void myRectangleWasClicked()
    {
        if( beep != null )
            beep.play();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM